You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md
+67-1Lines changed: 67 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,4 +30,70 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
30
30
*`1 <= k <= n <= 5000`
31
31
*`0 <= Node.val <= 1000`
32
32
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
+
publicclassSolution {
54
+
publicListNodereverseKGroup(ListNodehead, intk) {
55
+
// Create a dummy node and point its next to the head
56
+
ListNode dummy =newListNode(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
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