public static LinkedList reverseK(LinkedList head, int k){
LinkedList prev = null,next=null;
LinkedList curr = head;
int count = 0;
while (curr !=null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
if(next!=null)
head.next = reverseK(next,k);
return prev;
}
No comments:
Post a Comment