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: leetcode/92.reverse-linked-list-ii.md
+51-68Lines changed: 51 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -2,67 +2,85 @@
2
2
3
3
**Idea!** We find the part to reverse and the before / after node of reverse list, reuse modified `reverseLinkedList(node)` with length to reverse, and relink the before / after node pointer to correct.
4
4
5
+
For example:
6
+
5
7
```js
6
8
1->2->3->4->5
7
9
left =2, right =4
8
10
9
-
// Traverse list to find the before / after pointer
10
-
before =1
11
-
after =5
11
+
// Traverse list to find the node of before left, after right and last node of reversed list
12
+
beforeLeft =1
13
+
afterRight =5
14
+
last =2
12
15
13
16
// Reverse from left to right
14
17
reversedHead =reverseLinkedList(2->3->4), return4
15
18
16
19
// Relink before and after pointer to reverse list
For the example: `1 -> 2 -> 3 -> 4 -> 5`, left = 2, right = 4, we have to find `1 -> 2` and `4 -> 5`, and reverse between `2 -> 3 -> 4` and relink `1` to the new head of reversed list and relink the last node of reversed list to `5`.
0 commit comments