Java Implementation:
static listNode deleteMNodeAfterN(listNode head, int M, int N){
if(head == null) return head;
listNode curr = head, next = head;
while(next != null){
for(int i = 0 ; curr != null && i < M-1; i++)
curr = curr.next;
if(curr == null) return head;
next = curr;
for(int i = 0; next != null && i < N; i++)
next = next.next;
if(next == null)
{
curr.next = null;
return head;
}
curr.next = next.next;
curr = curr.next;
}
return head;
}
Reference:
http://www.geeksforgeeks.org/delete-n-nodes-after-m-nodes-of-a-linked-list/
No comments:
Post a Comment