Sunday, September 8, 2013

Remove K nodes from back and add in front in Link List.

WAP such that remove K elements from back and add in front.
Example:
Sample Input : 
Link list :10->20->100->30->50->5->70->234
Given K : 3
Sample Output:
5->70->234->10->20->100->30->50
Algo:
  1. Take two pointer both pointing head at beginning.
  2. Move firstPointer to K node.
  3. Now move both first and second pointer while firstPointer != null.
  4. Here secondPointer will point to Kth Node from Last.
  5. Make node of secondPointer as head of Link list.

Source Code:

No comments:

Post a Comment