Delete alternate nodes of a Linked List in JAVA
JAVA Program to Delete Alternate Nodes of a Singly Linked List
In this page we’ll take a look at a JAVA program in which we’ll delete alternate nodes in a singly Linked List starting fro the second node of the list. We’ll simply have to just change the address stored in the element before the element that is to be deleted and change it to the address of the element that is after the element, to be deleted.
Algorithm
The basic idea is that we keep track of previous of the node to be deleted and change its next link.
- Take two pointers- nm1 and n. nm1 represents the previous of the node to be deleted. n represents the actual node to be deleted.
- Inialize nm1 with head and the nth node with next of the head.
- While nm1 and n are not null, change the next link of previous node (nm1) to next of the node n. Update nm1 with the next of nth node. Also update the nth node if nm1 is not null.
- With the end of while loop in step 4, all alternate nodes would be deleted.
Code in JAVA Programming Language to Delete Alternate Nodes from a Linked List
Run
import java.util.*; class LinkedList { private class Node { int data; Node next; // Node constructor // There are two fields in the node- data and address of next node public Node (int data, Node next) { this.data = data; this.next = next; } } private Node head; private Node tail; private int size; // Linked list constructor public LinkedList () { this.head = null; this.tail = null; this.size = 0; } // Function to find the size of linked list public int size () { return this.size; } // Function to check whether linked list is empty or not public boolean isEmpty () { return this.size () == 0; } // Function to traverse and print the linked list public void display () { Node temp = head; while (temp != null) { System.out.print (temp.data + "-->"); // Set temp to point to the next node temp = temp.next; } System.out.println ("END"); } // Function to add a node in beginning of linked list public void addFirst (int item) { // Create a temp node which points to head Node temp = new Node (item, head); // If linked list is empty, temp is the head and tail node both if (this.size == 0) { this.head = this.tail = temp; } // else set the head such that it now points to temp node else { this.head = temp; } this.size++; } public void deleteAlternateNodes () { this.deleteAlternateNodes (this.head); } //Function to delete alternate nodes in linked list private void deleteAlternateNodes (Node node) { //Base case if (node == null) { return; } Node nm1 = this.head; Node n = nm1.next; while (nm1 != null && n != null) { nm1.next = n.next; nm1 = n.next; if (nm1 != null) { n = nm1.next; } } } } public class Main { public static void main (String[]args) throws Exception { LinkedList ll = new LinkedList (); ll.addFirst (70); ll.addFirst (60); ll.addFirst (50); ll.addFirst (40); ll.addFirst (30); ll.addFirst (20); ll.addFirst (10); ll.display (); ll.deleteAlternateNodes (); ll.display (); } }
10-->20-->30-->40-->50-->60-->70-->END 10-->30-->50-->70-->END
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
public void deletealternate(){
node pre=head;
int size=size();
if(size==0){
System.out.println(“invalid deletion”);
}
if(size%2!=0){
while(pre.next!=null){
pre.next=pre.next.next;
pre=pre.next;
}
}
else{
while(pre!=null){
pre.next=pre.next.next;
pre=pre.next;
}
}
}