Traverse the linked list recursively and remove every alternate node. At each step, adjust the current node’s next pointer to bypass the next node, effectively deleting it. Then move to the next valid node using recursion and repeat the process until the end of the list is reached.
Algorithm:
If the node is NULL or node->next is NULL, stop recursion.
Store the next node to be deleted in a temporary pointer: temp = node->next
Update the link to skip the node: node->next = temp->next
Delete the skipped node: delete(temp)
Move to the next valid node using recursion: deleteAlt(node->next)
Note : temp and delete(temp) are needed in C and C++ only as automatic garbage collection does not happen there.
C++
#include<iostream>usingnamespacestd;// A linked list node classNode{public:intdata;Node*next;Node(intval){data=val;next=NULL;}};// deletes alternate nodes voiddeleteAlt(Node*head){if(head==NULL)return;Node*prev=head;Node*node=head->next;while(prev!=NULL&&node!=NULL){prev->next=node->next;deletenode;prev=prev->next;if(prev!=NULL)node=prev->next;}}// print listvoidprintList(Node*node){while(node!=NULL){cout<<node->data<<" ";node=node->next;}}intmain(){//linked list: 1->2->3->4->5Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);cout<<"List before calling deleteAlt() \n";printList(head);deleteAlt(head);cout<<"\nList after calling deleteAlt() \n";printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// A linked list node structNode{intdata;structNode*next;};// Function to create a new nodestructNode*newNode(intdata){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=data;node->next=NULL;returnnode;}// deletes alternate nodes voiddeleteAlt(structNode*head){if(head==NULL)return;structNode*prev=head;structNode*node=head->next;while(prev!=NULL&&node!=NULL){prev->next=node->next;free(node);prev=prev->next;if(prev!=NULL)node=prev->next;}}// print listvoidprintList(structNode*node){while(node!=NULL){printf("%d ",node->data);node=node->next;}}intmain(){//linked list: 1->2->3->4->5structNode*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);printf("List before calling deleteAlt() \n");printList(head);deleteAlt(head);printf("\nList after calling deleteAlt() \n");printList(head);return0;}
Java
importjava.io.*;// A linked list node classNode{intdata;Nodenext;Node(intval){data=val;next=null;}}classLinkedList{// deletes alternate nodes voiddeleteAlt(Nodehead){if(head==null)return;Nodeprev=head;Nodenode=head.next;while(prev!=null&&node!=null){prev.next=node.next;node=null;prev=prev.next;if(prev!=null)node=prev.next;}}// print listvoidprintList(Nodenode){while(node!=null){System.out.print(node.data+" ");node=node.next;}}publicstaticvoidmain(String[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);LinkedListlist=newLinkedList();System.out.println("List before calling deleteAlt()");list.printList(head);list.deleteAlt(head);System.out.println("\nList after calling deleteAlt()");list.printList(head);}}
Python
# A linked list nodeclassNode:def__init__(self,val):self.data=valself.next=None# deletes alternate nodesdefdeleteAlt(head):ifheadisNone:returnprev=headnode=head.nextwhileprevisnotNoneandnodeisnotNone:prev.next=node.nextnode=Noneprev=prev.nextifprevisnotNone:node=prev.next# print listdefprintList(node):whilenodeisnotNone:print(node.data,end=" ")node=node.next# mainif__name__=='__main__':# linked list: 1->2->3->4->5head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)print("List before calling deleteAlt()\n")printList(head)print()deleteAlt(head)print("List after calling deleteAlt()\n")printList(head)print()
C#
usingSystem;// A linked list nodepublicclassNode{publicintdata;publicNodenext;publicNode(intval){data=val;next=null;}}publicclassProgram{// deletes alternate nodespublicstaticvoiddeleteAlt(Nodehead){if(head==null)return;Nodeprev=head;Nodenode=head.next;while(prev!=null&&node!=null){prev.next=node.next;// unlink nodeprev=prev.next;if(prev!=null)node=prev.next;}}// print listpublicstaticvoidprintList(Nodenode){while(node!=null){Console.Write(node.data+" ");node=node.next;}}publicstaticvoidMain(){// linked list: 1->2->3->4->5Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);Console.WriteLine("List before calling deleteAlt()");printList(head);deleteAlt(head);Console.WriteLine("\nList after calling deleteAlt()");printList(head);}}
JavaScript
classNode{constructor(val){this.data=val;this.next=null;}}// deletes alternate nodesfunctiondeleteAlt(head){if(head===null){return;}letprev=head;letnode=head.next;while(prev!==null&&node!==null){prev.next=node.next;node=null;prev=prev.next;if(prev!==null){node=prev.next;}}}// print listfunctionprintList(node){while(node!==null){console.log(node.data+" ");node=node.next;}}// Main executionlethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);console.log("List before calling deleteAlt()");printList(head);deleteAlt(head);console.log("\nList after calling deleteAlt()");printList(head);
Output
List before calling deleteAlt()
1 2 3 4 5
List after calling deleteAlt()
1 3 5
Iterative Approach - O(n) Time O(1) Space
Maintain a pointer to the node just before the one to be deleted, Update its next link to skip the unwanted node. Then move forward and repeat the process for the remaining list.
Algorithm:
Start from the head of the linked list.
If the list is empty (head == NULL), stop.
Set prev = head and node = head->next.
Traverse the list while both prev and node are not NULL.
Update the link to skip the node to be deleted: prev->next = node->next
Delete the node: delete(node)
Move prev to the next valid node: prev = prev->next
If prev is not NULL, update: node = prev->next
Note : delete(mode) ste needed in C and C++ only as automatic garbage collection does not happen there.
C++
#include<bits/stdc++.h>usingnamespacestd;// A linked list node classNode{public:intdata;Node*next;Node(intval){data=val;next=NULL;}};// deletes alternate nodes voiddeleteAlt(Node*head){if(head==NULL)return;Node*prev=head;Node*node=head->next;while(prev!=NULL&&node!=NULL){prev->next=node->next;delete(node);prev=prev->next;if(prev!=NULL)node=prev->next;}}// print linked list voidprintList(Node*node){while(node!=NULL){cout<<node->data<<" ";node=node->next;}}// Driver code intmain(){// list: 1->2->3->4->5Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);cout<<"List before deletion:\n";printList(head);deleteAlt(head);cout<<"\nList after deleting:\n";printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// A linked list node structNode{intdata;structNode*next;};structNode*newNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->next=NULL;returnnode;}// deletes alternate nodes voiddeleteAlt(structNode*head){if(head==NULL)return;structNode*prev=head;structNode*node=head->next;while(prev!=NULL&&node!=NULL){prev->next=node->next;free(node);prev=prev->next;if(prev!=NULL)node=prev->next;}}// print linked list voidprintList(structNode*node){while(node!=NULL){printf("%d ",node->data);node=node->next;}}// Driver code intmain(){// list: 1->2->3->4->5structNode*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);printf("List before deletion:\n");printList(head);deleteAlt(head);printf("\nList after deleting:\n");printList(head);return0;}
Java
importjava.util.*;// A linked list node classNode{intdata;Nodenext;Node(intval){data=val;next=null;}}publicclassLinkedList{// deletes alternate nodes staticvoiddeleteAlt(Nodehead){if(head==null)return;Nodeprev=head;Nodenode=head.next;while(prev!=null&&node!=null){prev.next=node.next;node=null;// for garbage collectionprev=prev.next;if(prev!=null)node=prev.next;}}// print linked list staticvoidprintList(Nodenode){while(node!=null){System.out.print(node.data+" ");node=node.next;}}// Driver code publicstaticvoidmain(String[]args){// list: 1->2->3->4->5Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);System.out.println("List before deletion:");printList(head);deleteAlt(head);System.out.println("\nList after deleting:");printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=None# deletes alternate nodesdefdeleteAlt(head):ifheadisNone:returnprev=headnode=head.nextwhileprevisnotNoneandnodeisnotNone:prev.next=node.nextnode=Noneprev=prev.nextifprevisnotNone:node=prev.next# print linked listdefprintList(node):whilenodeisnotNone:print(node.data,end=" ")node=node.next# Driver codeif__name__=='__main__':# list: 1->2->3->4->5head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)print("List before deletion:\n")printList(head)deleteAlt(head)print("\nList after deleting:\n")printList(head)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGfG{// deletes alternate nodespublicstaticvoiddeleteAlt(Nodehead){if(head==null)return;Nodeprev=head;Nodenode=head.next;while(prev!=null&&node!=null){prev.next=node.next;// unlinkprev=prev.next;if(prev!=null)node=prev.next;}}// print linked listpublicstaticvoidprintList(Nodenode){while(node!=null){Console.Write(node.data+" ");node=node.next;}}// Driver codepublicstaticvoidMain(){// list: 1->2->3->4->5Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);Console.WriteLine("List before deletion:");printList(head);deleteAlt(head);Console.WriteLine("\nList after deleting:");printList(head);}}
JavaScript
/* A linked list node */functionNode(data){this.data=data;this.next=null;}/* deletes alternate nodes */functiondeleteAlt(head){if(head==null){return;}letprev=head;letnode=head.next;while(prev!=null&&node!=null){prev.next=node.next;node=null;// delete(node);prev=prev.next;if(prev!=null){node=prev.next;}}}/* print linked list */functionprintList(node){while(node!=null){console.log(node.data+" ");node=node.next;}}/* Driver code */(functionmain(){/* list: 1->2->3->4->5 */lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);console.log("List before deletion:");printList(head);deleteAlt(head);console.log("\nList after deleting:");printList(head);})();
Output
List before deletion:
1 2 3 4 5
List after deleting:
1 3 5
Please write comments if you find the above code/algorithm incorrect, or find better ways to solve the same problem.