Reverse LinkedList in group of K
Idea is to use recursion and reverse K list iteratively and use recursive method to link k diff. reversed linkedList
Solution :
public static Node reverse(Node head, int k)
{
if(head == null) return head;
int cur = 0;
Node itr = head;
Node next = null;
Node prev = null;
while(cur<k && itr!=null) {
next = itr.next;
itr.next = prev;
prev = itr;
itr = next;
++cur;
}
head.next = reverse(itr, k);
return prev;
}
0 Comments:
Post a Comment