Skip to content

Commit 6faa290

Browse files
authored
Added task explanation
1 parent a4c0dee commit 6faa290

File tree

1 file changed

+67
-1
lines changed
  • src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group

1 file changed

+67
-1
lines changed

src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,70 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
3030
* `1 <= k <= n <= 5000`
3131
* `0 <= Node.val <= 1000`
3232

33-
**Follow-up:** Can you solve the problem in `O(1)` extra memory space?
33+
**Follow-up:** Can you solve the problem in `O(1)` extra memory space?
34+
35+
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:
36+
37+
1. Define a `Solution` class.
38+
2. 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.
39+
3. 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.
40+
4. 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.
41+
5. Initialize pointers `prev`, `curr`, `next`, and `tail`. Set `prev` and `tail` to the dummy node, and `curr` to the head of the input list.
42+
6. Iterate through the list:
43+
- Move `curr` k steps forward. If it's not possible (i.e., there are less than k nodes left), break the loop.
44+
- Set `next` to the `next` pointer of `curr`.
45+
- Reverse the sublist from `curr` to `next` using the `reverse` method. Update `prev` and `tail` accordingly.
46+
- Move `prev` and `tail` k steps forward to the last node of the reversed sublist.
47+
- Move `curr` to `next`.
48+
7. Return the `next` pointer of the dummy node, which points to the head of the modified list.
49+
50+
Here's the implementation:
51+
52+
```java
53+
public class Solution {
54+
public ListNode reverseKGroup(ListNode head, int k) {
55+
// Create a dummy node and point its next to the head
56+
ListNode dummy = new ListNode(0);
57+
dummy.next = head;
58+
59+
// Initialize pointers
60+
ListNode prev = dummy, curr = head, next, tail;
61+
62+
// Iterate through the list and reverse in groups of k
63+
while (true) {
64+
// Move curr k steps forward
65+
tail = prev;
66+
for (int i = 0; i < k; i++) {
67+
tail = tail.next;
68+
if (tail == null) return dummy.next; // Less than k nodes left
69+
}
70+
71+
next = tail.next; // Save the next pointer of the sublist
72+
tail.next = null; // Disconnect the sublist from the rest of the list
73+
74+
// Reverse the sublist and update prev and tail pointers
75+
prev.next = reverse(curr, tail);
76+
tail.next = next; // Connect the reversed sublist back to the rest of the list
77+
78+
// Move prev, tail, and curr to the next group
79+
prev = curr;
80+
curr = next;
81+
}
82+
}
83+
84+
// Helper method to reverse a sublist from head to tail
85+
private ListNode reverse(ListNode head, ListNode tail) {
86+
ListNode prev = null, curr = head, next;
87+
while (curr != null) {
88+
next = curr.next;
89+
curr.next = prev;
90+
prev = curr;
91+
curr = next;
92+
if (prev == tail) break;
93+
}
94+
return prev; // Return the new head of the reversed sublist
95+
}
96+
}
97+
```
98+
99+
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.

0 commit comments

Comments
 (0)