|
| 1 | +--- |
| 2 | +id: reverse-linked-list-II |
| 3 | +title: Reverse Linked List II |
| 4 | +sidebar_label: 0092 - Reverse Linked List II |
| 5 | +tags: |
| 6 | + - Linked List |
| 7 | + - Leetcode |
| 8 | +description: "This is a solution to the Reverse Linked List II problem on LeetCode." |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Statement |
| 12 | + |
| 13 | +Given the head of a singly linked list and two integers left and right where `left <= right`, reverse the nodes of the list from position left to position right, and return the reversed list. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +``` |
| 20 | +Input: head = [1,2,3,4,5], left = 2, right = 4 |
| 21 | +Output: [1,4,3,2,5] |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: head = [5], left = 1, right = 1 |
| 28 | +Output: [5] |
| 29 | +``` |
| 30 | + |
| 31 | +### Constraints: |
| 32 | + |
| 33 | +- The number of nodes in the list is `n`. |
| 34 | +- $1 <= n <= 500$ |
| 35 | +- $-500 <= Node.val <= 500$ |
| 36 | +- $1 <= left <= right <= n$ |
| 37 | + |
| 38 | +### Algorithm |
| 39 | + |
| 40 | +1. Create a dummy node pointing to the head of the list to handle edge cases easily. |
| 41 | +2. Find the node just before the `left` position (`leftNode`) and the node at the `left` position (`curr`). |
| 42 | +3. Reverse the sublist from `left` to `right`. |
| 43 | +4. Reconnect the reversed sublist back to the list. |
| 44 | +5. Return the new head of the list (`dummy.next`). |
| 45 | + |
| 46 | +### Pseudocode |
| 47 | + |
| 48 | +``` |
| 49 | +function reverseLinkedList(head, left, right): |
| 50 | + if head is null: |
| 51 | + return null |
| 52 | +
|
| 53 | + dummy = new ListNode(0) |
| 54 | + dummy.next = head |
| 55 | + leftNode = dummy |
| 56 | + curr = head |
| 57 | +
|
| 58 | + for i from 0 to left-2: |
| 59 | + leftNode = leftNode.next |
| 60 | + curr = curr.next |
| 61 | +
|
| 62 | + subhead = curr |
| 63 | + preNode = null |
| 64 | +
|
| 65 | + for i from 0 to right-left: |
| 66 | + nextNode = curr.next |
| 67 | + curr.next = preNode |
| 68 | + preNode = curr |
| 69 | + curr = nextNode |
| 70 | +
|
| 71 | + leftNode.next = preNode |
| 72 | + subhead.next = curr |
| 73 | +
|
| 74 | + return dummy.next |
| 75 | +``` |
| 76 | + |
| 77 | +### Python |
| 78 | + |
| 79 | +```python |
| 80 | +class ListNode: |
| 81 | + def __init__(self, val=0, next=None): |
| 82 | + self.val = val |
| 83 | + self.next = next |
| 84 | + |
| 85 | +class Solution: |
| 86 | + def reverseLinkedList(self, head: ListNode, left: int, right: int) -> ListNode: |
| 87 | + if not head: |
| 88 | + return None |
| 89 | + |
| 90 | + dummy = ListNode(0) |
| 91 | + dummy.next = head |
| 92 | + leftNode = dummy |
| 93 | + curr = head |
| 94 | + |
| 95 | + for i in range(left - 1): |
| 96 | + leftNode = leftNode.next |
| 97 | + curr = curr.next |
| 98 | + |
| 99 | + subhead = curr |
| 100 | + preNode = None |
| 101 | + |
| 102 | + for i in range(right - left + 1): |
| 103 | + nextNode = curr.next |
| 104 | + curr.next = preNode |
| 105 | + preNode = curr |
| 106 | + curr = nextNode |
| 107 | + |
| 108 | + leftNode.next = preNode |
| 109 | + subhead.next = curr |
| 110 | + |
| 111 | + return dummy.next |
| 112 | +``` |
| 113 | + |
| 114 | +### Java |
| 115 | + |
| 116 | +```java |
| 117 | +class ListNode { |
| 118 | + int val; |
| 119 | + ListNode next; |
| 120 | + ListNode() {} |
| 121 | + ListNode(int val) { this.val = val; } |
| 122 | + ListNode(int val, ListNode next) { this.val = val; this.next = next; } |
| 123 | +} |
| 124 | + |
| 125 | +class Solution { |
| 126 | + public ListNode reverseLinkedList(ListNode head, int left, int right) { |
| 127 | + if (head == null) return null; |
| 128 | + |
| 129 | + ListNode dummy = new ListNode(0); |
| 130 | + dummy.next = head; |
| 131 | + ListNode leftNode = dummy; |
| 132 | + ListNode curr = head; |
| 133 | + |
| 134 | + for (int i = 0; i < left - 1; i++) { |
| 135 | + leftNode = leftNode.next; |
| 136 | + curr = curr.next; |
| 137 | + } |
| 138 | + |
| 139 | + ListNode subhead = curr; |
| 140 | + ListNode preNode = null; |
| 141 | + |
| 142 | + for (int i = 0; i <= right - left; i++) { |
| 143 | + ListNode nextNode = curr.next; |
| 144 | + curr.next = preNode; |
| 145 | + preNode = curr; |
| 146 | + curr = nextNode; |
| 147 | + } |
| 148 | + |
| 149 | + leftNode.next = preNode; |
| 150 | + subhead.next = curr; |
| 151 | + |
| 152 | + return dummy.next; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +### C++ |
| 158 | + |
| 159 | +```cpp |
| 160 | +class ListNode { |
| 161 | +public: |
| 162 | + int val; |
| 163 | + ListNode *next; |
| 164 | + ListNode() : val(0), next(nullptr) {} |
| 165 | + ListNode(int x) : val(x), next(nullptr) {} |
| 166 | + ListNode(int x, ListNode *next) : val(x), next(next) {} |
| 167 | +}; |
| 168 | + |
| 169 | +class Solution { |
| 170 | +public: |
| 171 | + ListNode* reverseLinkedList(ListNode* head, int left, int right) { |
| 172 | + if (!head) return nullptr; |
| 173 | + |
| 174 | + ListNode* dummy = new ListNode(0); |
| 175 | + dummy->next = head; |
| 176 | + |
| 177 | + ListNode* leftNode = dummy; |
| 178 | + ListNode* curr = head; |
| 179 | + |
| 180 | + for (int i = 0; i < left - 1; i++) { |
| 181 | + leftNode = leftNode->next; |
| 182 | + curr = curr->next; |
| 183 | + } |
| 184 | + |
| 185 | + ListNode* subhead = curr; |
| 186 | + ListNode* preNode = nullptr; |
| 187 | + |
| 188 | + for (int i = 0; i <= right - left; i++) { |
| 189 | + ListNode* nextNode = curr->next; |
| 190 | + curr->next = preNode; |
| 191 | + preNode = curr; |
| 192 | + curr = nextNode; |
| 193 | + } |
| 194 | + |
| 195 | + leftNode->next = preNode; |
| 196 | + subhead->next = curr; |
| 197 | + |
| 198 | + return dummy->next; |
| 199 | + } |
| 200 | +}; |
| 201 | +``` |
| 202 | + |
| 203 | +### JavaScript |
| 204 | + |
| 205 | +```javascript |
| 206 | +class ListNode { |
| 207 | + constructor(val = 0, next = null) { |
| 208 | + this.val = val; |
| 209 | + this.next = next; |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +var reverseLinkedList = function (head, left, right) { |
| 214 | + if (!head) return null; |
| 215 | + |
| 216 | + let dummy = new ListNode(0); |
| 217 | + dummy.next = head; |
| 218 | + let leftNode = dummy; |
| 219 | + let curr = head; |
| 220 | + |
| 221 | + for (let i = 0; i < left - 1; i++) { |
| 222 | + leftNode = leftNode.next; |
| 223 | + curr = curr.next; |
| 224 | + } |
| 225 | + |
| 226 | + let subhead = curr; |
| 227 | + let preNode = null; |
| 228 | + |
| 229 | + for (let i = 0; i <= right - left; i++) { |
| 230 | + let nextNode = curr.next; |
| 231 | + curr.next = preNode; |
| 232 | + preNode = curr; |
| 233 | + curr = nextNode; |
| 234 | + } |
| 235 | + |
| 236 | + leftNode.next = preNode; |
| 237 | + subhead.next = curr; |
| 238 | + |
| 239 | + return dummy.next; |
| 240 | +}; |
| 241 | +``` |
0 commit comments