[Naive Approach] Using Recursive Traversal - O(n) Time and O(n/k) Space
The idea is to traverse the linked list while maintaining a counter to track node positions.
Every time the counter reaches k, update the next pointer of the previous node to skip the current kth node, effectively removing it from the list.
Continue this process until reaching the end of the list.
Working of Approach:
Start from the current node and move K - 1 nodes ahead.
If fewer than K nodes remain, return the current list.
Delete the K-th node and connect the previous node to the next node.
Recursively process the list starting after the deleted node.
Return the modified linked list.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*deleteK(Node*head,intK){// If the list is empty, return the head.if(head==nullptr)returnhead;Node*temp=head;Node*prev=nullptr;// Move to the K-th node.for(inti=1;i<K&&temp!=nullptr;i++){prev=temp;temp=temp->next;}// If fewer than K nodes remain, return the list.if(temp==nullptr)returnhead;// Delete the K-th node.if(prev==nullptr){head=temp->next;}else{prev->next=temp->next;}// Store the next node before deleting.Node*nextNode=temp->next;// Free memory.deletetemp;// Recursively process the remaining list.if(prev==nullptr)returndeleteK(nextNode,K);prev->next=deleteK(nextNode,K);returnhead;}voidprintList(Node*head){// Traverse and print the linked list.while(head!=nullptr){cout<<head->data;if(head->next!=nullptr)cout<<" -> ";head=head->next;}cout<<endl;}intmain(){// Create the linked list.Node*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);head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->next=newNode(7);head->next->next->next->next->next->next->next=newNode(8);head->next->next->next->next->next->next->next->next=newNode(9);head->next->next->next->next->next->next->next->next->next=newNode(10);intK=3;head=deleteK(head,K);printList(head);return0;}
Java
importjava.util.*;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteK(Nodehead,intK){// If the list is empty, return the head.if(head==null)returnhead;Nodetemp=head;Nodeprev=null;// Move to the K-th node.for(inti=1;i<K&&temp!=null;i++){prev=temp;temp=temp.next;}// If fewer than K nodes remain, return the list.if(temp==null)returnhead;// Delete the K-th node.if(prev==null){head=temp.next;}else{prev.next=temp.next;}// Store the next node before deleting.NodenextNode=temp.next;// Free memory.temp=null;// Recursively process the remaining list.if(prev==null)returndeleteK(nextNode,K);prev.next=deleteK(nextNode,K);returnhead;}publicstaticvoidprintList(Nodehead){// Traverse and print the linked list.while(head!=null){System.out.print(head.data);if(head.next!=null)System.out.print(" -> ");head=head.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create the linked list.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);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);intK=3;head=deleteK(head,K);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefdeleteK(head,K):# If the list is empty, return the head.ifheadisNone:returnheadtemp=headprev=None# Move to the K-th node.foriinrange(1,K):iftempisNone:breakprev=temptemp=temp.next# If fewer than K nodes remain, return the list.iftempisNone:returnhead# Delete the K-th node.ifprevisNone:head=temp.nextelse:prev.next=temp.next# Store the next node before deleting.nextNode=temp.next# Free memory.temp=None# Recursively process the remaining list.ifprevisNone:returndeleteK(nextNode,K)prev.next=deleteK(nextNode,K)returnheaddefprintList(head):# Traverse and print the linked list.whileheadisnotNone:print(head.data,end="")ifhead.nextisnotNone:print(" -> ",end="")head=head.nextprint()if__name__=="__main__":# Create the linked list.head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(6)head.next.next.next.next.next.next=Node(7)head.next.next.next.next.next.next.next=Node(8)head.next.next.next.next.next.next.next.next=Node(9)head.next.next.next.next.next.next.next.next.next=Node(10)K=3head=deleteK(head,K)printList(head)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteK(Nodehead,intK){// If the list is empty, return the head.if(head==null)returnhead;Nodetemp=head;Nodeprev=null;// Move to the K-th node.for(inti=1;i<K&&temp!=null;i++){prev=temp;temp=temp.next;}// If fewer than K nodes remain, return the list.if(temp==null)returnhead;// Delete the K-th node.if(prev==null){head=temp.next;}else{prev.next=temp.next;}// Store the next node before deleting.NodenextNode=temp.next;// Free memory.temp=null;// Recursively process the remaining list.if(prev==null)returndeleteK(nextNode,K);prev.next=deleteK(nextNode,K);returnhead;}publicstaticvoidprintList(Nodehead){// Traverse and print the linked list.while(head!=null){Console.Write(head.data);if(head.next!=null)Console.Write(" -> ");head=head.next;}Console.WriteLine();}publicstaticvoidMain(){// Create the linked list.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);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);intK=3;head=deleteK(head,K);printList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functiondeleteK(head,K){// If the list is empty, return the head.if(!head)returnhead;lettemp=head;letprev=null;// Move to the K-th node.for(leti=1;i<K&&temp;i++){prev=temp;temp=temp.next;}// If fewer than K nodes remain, return the list.if(!temp)returnhead;// Delete the K-th node.if(!prev){head=temp.next;}else{prev.next=temp.next;}// Store the next node before deleting.letnextNode=temp.next;// Free memory.temp=null;// Recursively process the remaining list.if(!prev)returndeleteK(nextNode,K);prev.next=deleteK(nextNode,K);returnhead;}functionprintList(head){// Traverse and print the linked list.letcurrent=head;while(current){process.stdout.write(current.data);if(current.next)process.stdout.write(" -> ");current=current.next;}console.log();}// Driver Code// Create the linked list.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);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);letK=3;head=deleteK(head,K);printList(head);
Output
1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10
[Expected Approach] Using Position-Based Single Traversal - O(n) Time and O(1) Space
The idea is to traverse the linked list while tracking positions. Whenever the next node is at a position divisible by K, delete it by updating the current node’s next pointer.
Working of Approach:
Start with temp at the head and count = 1.
Check whether temp->next is the K-th node.
If yes, remove temp->next by changing the link.
Otherwise, move temp to the next node.
Continue until the end of the linked list.
Let us understand with an example: Input: k = 3
Start with temp = 1 and count = 1. Since (1 + 1) % 3 != 0, move temp to 2.
Now count = 2. Since (2 + 1) % 3 == 0, the next node 3 is the 3rd node, so delete 3.
After deletion, keep temp at 2 and increment count to 3.
Continue traversing the original positions. The nodes at positions 6 and 9 are also deleted.
The final linked list is 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*deleteK(Node*head,intK){// Check if the list is empty or K is 1if(head==nullptr||K==1)returnnullptr;// Create a temporary node to traverse the listNode*temp=head;// Initialize a counter for counting nodesintcount=1;while(temp!=nullptr&&temp->next!=nullptr){// If next node is the K-th nodeif((count+1)%K==0){Node*nodeToDelete=temp->next;// Skip the K-th nodetemp->next=nodeToDelete->next;// Free memorydeletenodeToDelete;}else{// Move to the next nodetemp=temp->next;}count++;}// Return the modified listreturnhead;}voidprintList(Node*head){// Traverse and print the linked list.while(head!=nullptr){cout<<head->data;if(head->next!=nullptr)cout<<" -> ";head=head->next;}cout<<endl;}intmain(){// Create the linked list.Node*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);head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->next=newNode(7);head->next->next->next->next->next->next->next=newNode(8);head->next->next->next->next->next->next->next->next=newNode(9);head->next->next->next->next->next->next->next->next->next=newNode(10);intK=3;head=deleteK(head,K);printList(head);return0;}
Java
importjava.util.*;classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}publicclassGFG{staticNodedeleteK(Nodehead,intK){// Check if the list is empty or K is 1if(head==null||K==1)returnnull;// Create a temporary node to traverse the listNodetemp=head;// Initialize a counter for counting nodesintcount=1;while(temp!=null&&temp.next!=null){// If next node is the K-th nodeif((count+1)%K==0){NodenodeToDelete=temp.next;// Skip the K-th nodetemp.next=nodeToDelete.next;// Free memorynodeToDelete=null;}else{// Move to the next nodetemp=temp.next;}count++;}// Return the modified listreturnhead;}staticvoidprintList(Nodehead){// Traverse and print the linked list.while(head!=null){System.out.print(head.data);if(head.next!=null)System.out.print(" -> ");head=head.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create the linked list.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);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);intK=3;head=deleteK(head,K);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefdeleteK(head,K):# Check if the list is empty or K is 1ifheadisNoneorK==1:returnNone# Create a temporary node to traverse the listtemp=head# Initialize a counter for counting nodescount=1whiletempisnotNoneandtemp.nextisnotNone:# If next node is the K-th nodeif(count+1)%K==0:nodeToDelete=temp.next# Skip the K-th nodetemp.next=nodeToDelete.next# Free memorynodeToDelete=Noneelse:# Move to the next nodetemp=temp.nextcount+=1# Return the modified listreturnheaddefprintList(head):# Traverse and print the linked list.whileheadisnotNone:print(head.data,end="")ifhead.nextisnotNone:print(" -> ",end="")head=head.nextprint()if__name__=="__main__":# Create the linked list.head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)head.next.next.next.next.next=Node(6)head.next.next.next.next.next.next=Node(7)head.next.next.next.next.next.next.next=Node(8)head.next.next.next.next.next.next.next.next=Node(9)head.next.next.next.next.next.next.next.next.next=Node(10)K=3head=deleteK(head,K)printList(head)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteK(Nodehead,intK){// Check if the list is empty or K is 1if(head==null||K==1)returnnull;// Create a temporary node to traverse the listNodetemp=head;// Initialize a counter for counting nodesintcount=1;while(temp!=null&&temp.next!=null){// If next node is the K-th nodeif((count+1)%K==0){NodenodeToDelete=temp.next;// Skip the K-th nodetemp.next=nodeToDelete.next;// Free memorynodeToDelete=null;}else{// Move to the next nodetemp=temp.next;}count++;}// Return the modified listreturnhead;}publicstaticvoidprintList(Nodehead){// Traverse and print the linked list.while(head!=null){Console.Write(head.data);if(head.next!=null)Console.Write(" -> ");head=head.next;}Console.WriteLine();}publicstaticvoidMain(){// Create the linked list.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);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);intK=3;head=deleteK(head,K);printList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functiondeleteK(head,K){// Check if the list is empty or K is 1if(head===null||K===1)returnnull;// Create a temporary node to traverse the listlettemp=head;// Initialize a counter for counting nodesletcount=1;while(temp!==null&&temp.next!==null){// If next node is the K-th nodeif((count+1)%K===0){letnodeToDelete=temp.next;// Skip the K-th nodetemp.next=nodeToDelete.next;// Free memorynodeToDelete=null;}else{// Move to the next nodetemp=temp.next;}count++;}// Return the modified listreturnhead;}functionprintList(head){// Traverse and print the linked list.letcurrent=head;while(current!==null){process.stdout.write(current.data.toString());if(current.next!==null)process.stdout.write(" -> ");current=current.next;}console.log();}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.next=newNode(7);head.next.next.next.next.next.next.next=newNode(8);head.next.next.next.next.next.next.next.next=newNode(9);head.next.next.next.next.next.next.next.next.next=newNode(10);letK=3;head=deleteK(head,K);printList(head);