Class Solution
Hard
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.
You may not alter the values in the list’s nodes, only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Example 3:
Input: head = [1,2,3,4,5], k = 1
Output: [1,2,3,4,5]
Example 4:
Input: head = [1], k = 1
Output: [1]
Constraints:
- The number of nodes in the list is in the range
sz
. 1 <= sz <= 5000
0 <= Node.val <= 1000
1 <= k <= sz
Follow-up: Can you solve the problem in O(1) extra memory space?
To solve the “Reverse Nodes in k-Group” problem in Java with a Solution
class, we can reverse the nodes in groups of k using a recursive approach. Here are the steps:
- Define a
Solution
class. - Define a method named
reverseKGroup
that takes the head of a linked list and an integer k as input and returns the head of the modified list. - Define a helper method named
reverse
that takes the head and tail of a sublist as input and reverses the sublist in place. This method returns the new head of the sublist. - Create a dummy ListNode object and set its
next
pointer to the head of the input list. This dummy node will serve as the new head of the modified list. - Initialize pointers
prev
,curr
,next
, andtail
. Setprev
andtail
to the dummy node, andcurr
to the head of the input list. - Iterate through the list:
- Move
curr
k steps forward. If it’s not possible (i.e., there are less than k nodes left), break the loop. - Set
next
to thenext
pointer ofcurr
. - Reverse the sublist from
curr
tonext
using thereverse
method. Updateprev
andtail
accordingly. - Move
prev
andtail
k steps forward to the last node of the reversed sublist. - Move
curr
tonext
.
- Move
- Return the
next
pointer of the dummy node, which points to the head of the modified list.
Here’s the implementation:
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Create a dummy node and point its next to the head
ListNode dummy = new ListNode(0);
dummy.next = head;
// Initialize pointers
ListNode prev = dummy, curr = head, next, tail;
// Iterate through the list and reverse in groups of k
while (true) {
// Move curr k steps forward
tail = prev;
for (int i = 0; i < k; i++) {
tail = tail.next;
if (tail == null) return dummy.next; // Less than k nodes left
}
next = tail.next; // Save the next pointer of the sublist
tail.next = null; // Disconnect the sublist from the rest of the list
// Reverse the sublist and update prev and tail pointers
prev.next = reverse(curr, tail);
tail.next = next; // Connect the reversed sublist back to the rest of the list
// Move prev, tail, and curr to the next group
prev = curr;
curr = next;
}
}
// Helper method to reverse a sublist from head to tail
private ListNode reverse(ListNode head, ListNode tail) {
ListNode prev = null, curr = head, next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
if (prev == tail) break;
}
return prev; // Return the new head of the reversed sublist
}
}
This implementation provides a solution to the “Reverse Nodes in k-Group” problem in Java without modifying the values in the list’s nodes. It recursively reverses the nodes in groups of k.
-
Constructor Summary
-
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
reverseKGroup
-