Reverse Nodes in k-Group (H)

Keywords: less

Reverse Nodes in k-Group (H)

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

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

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

meaning of the title

If every k nodes in a given list are rearranged in reverse order, the values in the nodes can not be changed, only the nodes themselves can be changed, and only the space of O(1)O(1)O(1)O(1) can be used.

thinking

As a whole, Swap Nodes in Pairs (M) is similar to the solution of 24. Swap Nodes in Pairs (M), except that more than two nodes are arranged in reverse order. Because only the extra space of constant can be used, stack can not be used to achieve reverse order, and the way before inserting to the head node can be used directly to achieve reverse order.

Code Implementation - Recursion

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode pointer = head;
        // Determine whether the remaining node number reaches k and find the head node of the next k tuple
        for (int i = 0; i < k; i++) {
            if (pointer == null) {
                return head;
            }
            pointer = pointer.next;
        }

        ListNode nextHead = pointer;		// Record the header node of the next k tuple
        ListNode first = null;				// Recording the Head Node of Inverse List in Inverse Arrangement
        pointer = head;
        // Reverse processing
        while (pointer != nextHead) {
            ListNode temp = pointer.next;
            pointer.next = first;
            first = pointer;
            pointer = temp;
        }
        head.next = reverseKGroup(nextHead, k);
        
        return first;
    }
}

Code implementation - non-recursive

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode ans = new ListNode(0);
        ans.next = head;
        ListNode root = ans;		// Record the last node after the reverse order of a k tuple

        // Statistical Node Points
        ListNode pointer = head;
        int num = 0;
        while (pointer != null) {
            num++;
            pointer = pointer.next;
        }

        pointer = ans.next;
        while (num >= k) {
            ListNode first = null;			// Head Node in Inverse Sequence
            ListNode nextRoot = pointer;	// Record the first node before the current k-tuple reverse order
            // Reverse processing
            for (int i = 0; i < k; i++) {
                ListNode temp = pointer.next;
                pointer.next = first;
                first = pointer;
                pointer = temp;
            }
            root.next = first;
            root = nextRoot;
            num -= k;
        }
        root.next = pointer;		// Supplement the number of nodes less than k to the end of the list
        
        return ans.next;
    }
}

Posted by tomhilton on Sat, 05 Oct 2019 16:26:31 -0700