• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Delete alternate nodes of a linked list java

0 votes
26 views
Delete alternate nodes of a linked list java.
List:- [1 2 3 4 5 6 7]
Output:- [1 3 5 7 ]
asked Jan 23 in Data Structure And Algorithm by Nikhil Kumar

2 Answers

0 votes
With the help of following example we will learn how to delete alternate nodes of a linked list.

package com.dev.ds;



public class DeleteAlternate {

    Node head;
    /**
     * @param args
     */
    public static void main(String[] args) {
        DeleteAlternate deleteAlternate = new DeleteAlternate();
        deleteAlternate.insert(1);
        deleteAlternate.insert(2);
        deleteAlternate.insert(3);
        deleteAlternate.insert(4);
        deleteAlternate.insert(5);
       
        System.out.println("Original List: ");
        deleteAlternate.print();
        System.out.println("\nAfter removing duplicates List: ");
        deleteAlternate.deleteAlternateNode(deleteAlternate.head);
//        deleteAlternate.deleteAlternateNodeRec(deleteAlternate.head);
        deleteAlternate.print();
    }
   
   
    // Time complexity O(n);
        private void deleteAlternateNode(Node head2) {
            if(head2 == null)
                return;
            Node temp = head2;
            while(temp != null && temp.next != null){
                temp.next = temp.next.next;
                temp = temp.next;
            }
        }
       
    // Time complexity O(n);
        private void deleteAlternateNodeRec(Node head2) {
            if(head2 == null)
                return;
            Node temp = head2;
            if(temp != null && temp.next != null){
                temp.next = temp.next.next;
                deleteAlternateNodeRec(temp.next);
            }
        }


    public void insert(int data){
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
        }else{
            Node curr = head;
            while(curr.next != null){
                curr = curr.next;
            }
            curr.next = newNode;
        }
    }
   
    public void print(){
        if(head==null)
            return;
        Node curr = head;
        while(curr != null){
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
    }
   
    private class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
        }
    }

}
package com.dev.ds;



public class DeleteAlternate {

    Node head;
    /**
     * @param args
     */
    public static void main(String[] args) {
        DeleteAlternate deleteAlternate = new DeleteAlternate();
        deleteAlternate.insert(1);
        deleteAlternate.insert(2);
        deleteAlternate.insert(3);
        deleteAlternate.insert(4);
        deleteAlternate.insert(5);
       
        System.out.println("Original List: ");
        deleteAlternate.print();
        System.out.println("\nAfter removing duplicates List: ");
        deleteAlternate.deleteAlternateNode(deleteAlternate.head);
        deleteAlternate.print();
    }
   
   
    // Time complexity O(n);
        private void deleteAlternateNode(Node head2) {
            if(head2 == null)
                return;
            Node temp = head2;
            while(temp != null && temp.next != null){
                temp.next = temp.next.next;
                temp = temp.next;
            }
        }
       
    public void insert(int data){
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
        }else{
            Node curr = head;
            while(curr.next != null){
                curr = curr.next;
            }
            curr.next = newNode;
        }
    }
   
    public void print(){
        if(head==null)
            return;
        Node curr = head;
        while(curr != null){
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
    }
   
    private class Node{
        int data;
        Node next;
        Node(int data){
            this.data = data;
        }
    }

}
Output:-
Original List:
1 2 3 4 5
After removing duplicates List:
1 3 5
answered Jan 26 by ranju_12 (1,460 points)
0 votes
Another technique to delete alternate nodes from linked list. See the following java code:-

// Time complexity O(n);
        private void deleteAlternateNodeRec(Node head2) {
            if(head2 == null)
                return;
            Node temp = head2;
            if(temp != null && temp.next != null){
                temp.next = temp.next.next;
                deleteAlternateNodeRec(temp.next);
            }
        }
Output:-
Original List:
1 2 3 4 5
After removing duplicates List:
1 3 5
answered Jan 26 by ranju_12 (1,460 points)
...