|
| 1 | +## Problem |
| 2 | +https://leetcode.com/problems/reverse-nodes-in-k-group/ |
| 3 | + |
| 4 | +## Problem Description |
| 5 | +``` |
| 6 | +Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. |
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +Example: |
| 11 | +
|
| 12 | +Given this linked list: 1->2->3->4->5 |
| 13 | +
|
| 14 | +For k = 2, you should return: 2->1->4->3->5 |
| 15 | +
|
| 16 | +For k = 3, you should return: 3->2->1->4->5 |
| 17 | +
|
| 18 | +Note: |
| 19 | +
|
| 20 | +Only constant extra memory is allowed. |
| 21 | +You may not alter the values in the list's nodes, only nodes itself may be changed. |
| 22 | +
|
| 23 | +``` |
| 24 | + |
| 25 | +## Solution |
| 26 | +Traverse `linked list` from left to right, during traverse, group nodes in k, then reverse each group. |
| 27 | +How to reverse a linked list given start, end node? |
| 28 | + |
| 29 | +Reverse linked list: |
| 30 | + |
| 31 | +1. Initial a prev node `null` |
| 32 | + |
| 33 | +2. For each move, use temp node to keep current next node. |
| 34 | + |
| 35 | +3. During traverse, update current node pointing to previous node, update previous pointing to current node |
| 36 | + |
| 37 | +4. Update current to temp |
| 38 | + |
| 39 | +``` |
| 40 | +ListNode temp = curr.next; |
| 41 | +curr.next = prev; |
| 42 | +prev = curr; |
| 43 | +curr = temp; |
| 44 | +``` |
| 45 | + |
| 46 | +For example(as below pic): reverse the whole linked list `1->2->3->4->null` -> `4->3->2->1->null` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +Here Reverse each group(`k nodes`): |
| 51 | + |
| 52 | +1. First group, use `count` keep track linked list counts when traverse linked list |
| 53 | + |
| 54 | +2. Use `start` to keep track each group start node position. |
| 55 | + |
| 56 | +3. Use `end ` to keep track each group end node position |
| 57 | + |
| 58 | +4. Reverse(`k nodes`)AKA: `[start.next, end]`. |
| 59 | + |
| 60 | +5. After reverse, update `start` point to reversed group last node. and `end` point to `start` next node(`end=start.next`). |
| 61 | + |
| 62 | +6. If `counts % k != 0`, then `end` move to next(`end=end.next`), for each move`count+1`. |
| 63 | + |
| 64 | +For example(as below pic),`head=[1,2,3,4,5,6,7,8], k = 3` |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +>**NOTE**: Usually we create a `dummy node` to solve linked list problem, because head node may be changed during operation. |
| 71 | +for example: here `head updated from 1->3`, and `dummy (List(0)) ` keep the same. |
| 72 | +
|
| 73 | +#### Complexity Analysis |
| 74 | +- *Time Complexity:* `O(n) - n is number of Linked List` |
| 75 | +- *Space Complexity:* `O(1)` |
| 76 | + |
| 77 | +## Key Points |
| 78 | +1. create a dummy node, `dummy = ListNode(0)` |
| 79 | +2. Group linked list as `k=3`, keep track of start and end node for each group. |
| 80 | +3. Reverse each group, update start and end node references |
| 81 | +4. return `dummy.next`. |
| 82 | + |
| 83 | +## Code (`Java/Python3`) |
| 84 | +*Java Code* |
| 85 | +```java |
| 86 | +class ReverseKGroupsLinkedList { |
| 87 | + public ListNode reverseKGroup(ListNode head, int k) { |
| 88 | + if (head == null || k == 1) { |
| 89 | + return head; |
| 90 | + } |
| 91 | + ListNode dummy = new ListNode(0); |
| 92 | + dummy.next = head; |
| 93 | + |
| 94 | + ListNode start = dummy; |
| 95 | + ListNode end = head; |
| 96 | + int count = 0; |
| 97 | + while (end != null) { |
| 98 | + count++; |
| 99 | + // group |
| 100 | + if (count % k == 0) { |
| 101 | + // reverse linked list (start, end] |
| 102 | + start = reverse(start, end.next); |
| 103 | + end = start.next; |
| 104 | + } else { |
| 105 | + end = end.next; |
| 106 | + } |
| 107 | + } |
| 108 | + return dummy.next; |
| 109 | + } |
| 110 | + |
| 111 | + // reverse linked list from range (start, end], return last node. |
| 112 | + private ListNode reverse(ListNode pre, ListNode next) { |
| 113 | + ListNode last = pre.next; |
| 114 | + ListNode curr = last.next; |
| 115 | + |
| 116 | + while (curr != next) { |
| 117 | + last.next = curr.next; |
| 118 | + curr.next = pre.next; |
| 119 | + pre.next = curr; |
| 120 | + curr = last.next; |
| 121 | + } |
| 122 | + return last; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +*Python3 Cose* |
| 128 | +```python |
| 129 | +class Solution: |
| 130 | + def reverseKGroup(self, head: ListNode, k: int) -> ListNode: |
| 131 | + if head is None or k < 2: |
| 132 | + return head |
| 133 | + dummy = ListNode(0) |
| 134 | + dummy.next = head |
| 135 | + start = dummy |
| 136 | + end = head |
| 137 | + count = 0 |
| 138 | + while end: |
| 139 | + count += 1 |
| 140 | + if count % k == 0: |
| 141 | + start = self.reverse(start, end.next) |
| 142 | + end = start.next |
| 143 | + else: |
| 144 | + end = end.next |
| 145 | + return dummy.next |
| 146 | + |
| 147 | + def reverse(self, start, end): |
| 148 | + last = start.next |
| 149 | + curr = last.next |
| 150 | + while curr != end: |
| 151 | + last.next = curr.next |
| 152 | + curr.next = start.next |
| 153 | + start.next = curr |
| 154 | + curr = last.next |
| 155 | + return last |
| 156 | +``` |
| 157 | + |
| 158 | +## References |
| 159 | +- [Leetcode Discussion (yellowstone)](https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11440/Non-recursive-Java-solution-and-idea) |
| 160 | + |
| 161 | +## Extension |
| 162 | + |
| 163 | +- Require from right to left reverse nodes in k groups. **(ByteDance Interview)** |
| 164 | + |
| 165 | + Example,`1->2->3->4->5->6->7->8, k = 3`, |
| 166 | + |
| 167 | + From right to left, group as `k=3`: |
| 168 | + - `6->7->8` reverse to `8->7->6`, |
| 169 | + - `3->4->5` reverse to `5->4->3`. |
| 170 | + - `1->2` only has 2 nodes, which less than `k=3`, do nothing. |
| 171 | + |
| 172 | + return: `1->2->5->4->3->8->7->6` |
| 173 | + |
| 174 | +Here, we pre-process linked list, reverse it first, then using Reverse nodes in K groups solution: |
| 175 | + |
| 176 | +1. Reverse linked list |
| 177 | + |
| 178 | +2. From left to right, reverse linked list group as k nodes. |
| 179 | + |
| 180 | +3. Reverse step #2 linked list |
| 181 | + |
| 182 | +For example:`1->2->3->4->5->6->7->8, k = 3` |
| 183 | + |
| 184 | +1. Reverse linked list: `8->7->6->5->4->3->2->1` |
| 185 | + |
| 186 | +2. Reverse nodes in k groups: `6->7->8->3->4->5->2->1` |
| 187 | + |
| 188 | +3. Reverse step#2 linked list: `1->2->5->4->3->8->7->6` |
| 189 | + |
| 190 | +## Similar Problems |
| 191 | +- [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) |
0 commit comments