Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed.
private ListNode reverseListNode(ListNode head, ListNode tail){
if(head == tail || head == null) return head;
ListNode dummy = new ListNode(-1), p = head, tNext = tail.next;
dummy.next = head;
while(p.next != null && p.next != tNext){
ListNode next = p.next;
p.next = next.next;
next.next = dummy.next;
dummy.next = next;
}
return dummy.next;
}
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null || head.next == null || k <= 1) return head;
ListNode dummy = new ListNode(-1), b = head, e = head, pre = dummy;
dummy.next = head;
while(e != null){
int step = k;
while(-- step > 0 && e.next != null){
e = e.next;
}
// k's length > list/remain 's length
if(step > 0) break;
pre.next = reverseListNode(b, e);
while(pre.next != b.next)
pre = pre.next;
e = b.next;
b = b.next;
}
return dummy.next;
}