Traverse the List and Update Links - O(n) Time and O(1) Space
The idea is to traverse the linked list while repeatedly keeping the first m nodes and deleting the next n nodes. After skipping m nodes, simply update the next pointer of the last retained node to bypass the next n nodes. Repeat this process until the end of the list is reached.
Working of Approach:
Initialize a pointer temp to the head of the linked list.
Move temp forward by m - 1 nodes so that it points to the last node to be retained.
If the end of the list is reached, stop the traversal.
Store the node immediately after temp in another pointer.
Move this pointer forward by n nodes to skip the nodes that need to be deleted.
Update temp -> next to point to the first node after the deleted nodes.
Move temp to the next retained node and repeat the process until the end of the list is reached.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intval){data=val;next=nullptr;}};voidlinkDelete(Node*head,intn,intm){Node*temp=head;Node*t;while(temp!=nullptr){// Skip the first m nodesfor(intcount=1;count<m&&temp!=nullptr;count++)temp=temp->next;if(temp==nullptr)return;// Move to the first node to be deletedt=temp->next;// Skip the next n nodesfor(intcount=1;count<=n&&t!=nullptr;count++)t=t->next;// Link the retained nodes with the remaining listtemp->next=t;temp=t;}}voidprintList(Node*head){while(head!=nullptr){cout<<head->data;if(head->next!=nullptr)cout<<" -> ";head=head->next;}cout<<'\n';}intmain(){Node*head=newNode(9);head->next=newNode(1);head->next->next=newNode(3);head->next->next->next=newNode(5);head->next->next->next->next=newNode(9);head->next->next->next->next->next=newNode(4);head->next->next->next->next->next->next=newNode(10);head->next->next->next->next->next->next->next=newNode(1);intm=2;intn=1;linkDelete(head,n,m);printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}classGFG{staticvoidlinkDelete(Nodehead,intn,intm){Nodetemp=head;Nodet;while(temp!=null){// Skip the first m nodesfor(intcount=1;count<m&&temp!=null;count++)temp=temp.next;if(temp==null)return;// Move to the first node to be deletedt=temp.next;// Skip the next n nodesfor(intcount=1;count<=n&&t!=null;count++)t=t.next;// Link the retained nodes with the remaining listtemp.next=t;temp=t;}}staticvoidprintList(Nodehead){while(head!=null){System.out.print(head.data);if(head.next!=null)System.out.print(" -> ");head=head.next;}System.out.println();}publicstaticvoidmain(String[]args){Nodehead=newNode(9);head.next=newNode(1);head.next.next=newNode(3);head.next.next.next=newNode(5);head.next.next.next.next=newNode(9);head.next.next.next.next.next=newNode(4);head.next.next.next.next.next.next=newNode(10);head.next.next.next.next.next.next.next=newNode(1);intm=2;intn=1;linkDelete(head,n,m);printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=NonedeflinkDelete(head,n,m):temp=headwhiletemp:# Skip the first m nodesfor_inrange(1,m):iftempisNone:returntemp=temp.nextiftempisNone:return# Move to the first node to be deletedt=temp.next# Skip the next n nodesfor_inrange(n):iftisNone:breakt=t.next# Link the retained nodes with the remaining listtemp.next=ttemp=tdefprintList(head):whilehead:print(head.data,end="")ifhead.next:print(" -> ",end="")head=head.nextprint()if__name__=="__main__":head=Node(9)head.next=Node(1)head.next.next=Node(3)head.next.next.next=Node(5)head.next.next.next.next=Node(9)head.next.next.next.next.next=Node(4)head.next.next.next.next.next.next=Node(10)head.next.next.next.next.next.next.next=Node(1)m=2n=1linkDelete(head,n,m)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGFG{staticvoidlinkDelete(Nodehead,intn,intm){Nodetemp=head;Nodet;while(temp!=null){// Skip the first m nodesfor(intcount=1;count<m&&temp!=null;count++)temp=temp.next;if(temp==null)return;// Move to the first node to be deletedt=temp.next;// Skip the next n nodesfor(intcount=1;count<=n&&t!=null;count++)t=t.next;// Link the retained nodes with the remaining listtemp.next=t;temp=t;}}staticvoidPrintList(Nodehead){while(head!=null){Console.Write(head.data);if(head.next!=null)Console.Write(" -> ");head=head.next;}Console.WriteLine();}staticvoidMain(){Nodehead=newNode(9);head.next=newNode(1);head.next.next=newNode(3);head.next.next.next=newNode(5);head.next.next.next.next=newNode(9);head.next.next.next.next.next=newNode(4);head.next.next.next.next.next.next=newNode(10);head.next.next.next.next.next.next.next=newNode(1);intm=2;intn=1;linkDelete(head,n,m);PrintList(head);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functionlinkDelete(head,n,m){lettemp=head;while(temp!==null){// Skip the first m nodesfor(letcount=1;count<m&&temp!==null;count++)temp=temp.next;if(temp===null)return;// Move to the first node to be deletedlett=temp.next;// Skip the next n nodesfor(letcount=1;count<=n&&t!==null;count++)t=t.next;// Link the retained nodes with the remaining listtemp.next=t;temp=t;}}functionprintList(head){letans=[];while(head!==null){ans.push(head.data);head=head.next;}console.log(ans.join(" -> "));}// Driver Codelethead=newNode(9);head.next=newNode(1);head.next.next=newNode(3);head.next.next.next=newNode(5);head.next.next.next.next=newNode(9);head.next.next.next.next.next=newNode(4);head.next.next.next.next.next.next=newNode(10);head.next.next.next.next.next.next.next=newNode(1);letm=2;letn=1;linkDelete(head,n,m);printList(head);