leetcode

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.

For example, Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

The idea is to implement a reverseInBetween method which will reverse(1,5) - 0->1->2->3->4->5 to 0->4->3->2->1->5.

Then whenever reach the kth group node, reverse between the starting group node and the kth group node.

    //previous and next are exclusive
    public ListNode reverseInBetween(ListNode previous, ListNode next) {
        //after reverse, last node before next node
        ListNode last = previous.next;
        ListNode current = last.next;
        while (current!=next) {
            last.next = current.next;
            current.next = previous.next;
            previous.next = current;
            current = last.next;            
        }
        return last;
    }

    public ListNode reverseKGroup(ListNode head, int k) {
        if (head==null||head.next==null||k<2) {
            return head;
        }

        //0-1-2-3-4-5
        ListNode newHead = new ListNode(0);
        newHead.next = head;

        //previous need to point to newHead
        ListNode previous = newHead;
        int index = 1;
        while(head!=null) {            
            if (index % k==0) {
                //after first reverseInBetween, newHead will point to current head
                previous = reverseInBetween(previous, head.next);
                head = previous.next;
            } else {
                head = head.next;
            } 
            index = index + 1;
        }
        return newHead.next;
    }