File tree 5 files changed +157
-0
lines changed
092.Reverse Linked List II
5 files changed +157
-0
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,8 @@ Complete solutions to Leetcode problems, updated daily.
50
50
| 063 | [ Unique Paths II] ( https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II ) | ` Array ` , ` Dynamic Programming ` |
51
51
| 075 | [ Sort Colors] ( https://github.com/yanglbme/leetcode/tree/master/solution/075.Sort%20Colors ) | ` Array ` , ` Two Pointers ` , ` Sort ` |
52
52
| 082 | [ Remove Duplicates from Sorted List II] ( https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II ) | ` Linked List ` |
53
+ | 086 | [ Partition List] ( https://github.com/yanglbme/leetcode/tree/master/solution/086.Partition%20List ) | ` Linked List ` , ` Two Pointers ` |
54
+ | 092 | [ Reverse Linked List II] ( https://github.com/yanglbme/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II ) | ` Linked List ` |
53
55
| 094 | [ Binary Tree Inorder Traversal] ( https://github.com/yanglbme/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal ) | ` Hash Table ` , ` Stack ` , ` Tree ` |
54
56
| 144 | [ Binary Tree Preorder Traversal] ( https://github.com/yanglbme/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal ) | ` Stack ` , ` Tree ` |
55
57
| 153 | [ Find Minimum in Rotated Sorted Array] ( https://github.com/yanglbme/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array ) | ` Array ` , ` Binary Search ` |
Original file line number Diff line number Diff line change
1
+ ## 分隔链表
2
+ ### 题目描述
3
+
4
+ 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。
5
+
6
+ 你应当保留两个分区中每个节点的初始相对位置。
7
+
8
+ 示例:
9
+
10
+ ```
11
+ 输入: head = 1->4->3->2->5->2, x = 3
12
+ 输出: 1->2->2->4->3->5
13
+ ```
14
+
15
+ ### 解法
16
+ 维护 ` left ` , ` right ` 两个链表,遍历 ` head ` 链表,若对应元素值小于 ` x ` ,将该结点插入 ` left ` 链表中,否则插入 ` right ` 链表中。最后 ` right ` 尾部指向空,并将 ` left ` 尾部指向 ` right ` 头部,使得它们串在一起。
17
+
18
+ ``` java
19
+ /**
20
+ * Definition for singly-linked list.
21
+ * public class ListNode {
22
+ * int val;
23
+ * ListNode next;
24
+ * ListNode(int x) { val = x; }
25
+ * }
26
+ */
27
+ class Solution {
28
+ public ListNode partition (ListNode head , int x ) {
29
+ ListNode leftDummy = new ListNode (- 1 );
30
+ ListNode rightDummy = new ListNode (- 1 );
31
+
32
+ ListNode leftCur = leftDummy;
33
+ ListNode rightCur = rightDummy;
34
+
35
+ while (head != null ) {
36
+ if (head. val < x) {
37
+ leftCur. next = head;
38
+ leftCur = leftCur. next;
39
+ } else {
40
+ rightCur. next = head;
41
+ rightCur = rightCur. next;
42
+ }
43
+ head = head. next;
44
+ }
45
+
46
+ leftCur. next = rightDummy. next;
47
+ rightCur. next = null ;
48
+ return leftDummy. next;
49
+
50
+ }
51
+ }
52
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public ListNode partition (ListNode head , int x ) {
3
+ ListNode leftDummy = new ListNode (-1 );
4
+ ListNode rightDummy = new ListNode (-1 );
5
+
6
+ ListNode leftCur = leftDummy ;
7
+ ListNode rightCur = rightDummy ;
8
+
9
+ while (head != null ) {
10
+ if (head .val < x ) {
11
+ leftCur .next = head ;
12
+ leftCur = leftCur .next ;
13
+ } else {
14
+ rightCur .next = head ;
15
+ rightCur = rightCur .next ;
16
+ }
17
+ head = head .next ;
18
+ }
19
+
20
+ leftCur .next = rightDummy .next ;
21
+ rightCur .next = null ;
22
+ return leftDummy .next ;
23
+
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ ## 反转链表 II
2
+ ### 题目描述
3
+
4
+ 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
5
+
6
+ 说明:
7
+ 1 ≤ m ≤ n ≤ 链表长度。
8
+
9
+ 示例:
10
+
11
+ ```
12
+ 输入: 1->2->3->4->5->NULL, m = 2, n = 4
13
+ 输出: 1->4->3->2->5->NULL
14
+ ```
15
+
16
+ ### 解法
17
+ 利用头插法,对 [ m + 1, n] 范围内的元素逐一插入。
18
+
19
+ ``` java
20
+ /**
21
+ * Definition for singly-linked list.
22
+ * public class ListNode {
23
+ * int val;
24
+ * ListNode next;
25
+ * ListNode(int x) { val = x; }
26
+ * }
27
+ */
28
+ class Solution {
29
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
30
+ ListNode dummy = new ListNode (- 1 );
31
+ dummy. next = head;
32
+ ListNode pre = dummy;
33
+ int i = 0 ;
34
+ for (; i < m - 1 ; ++ i) {
35
+ pre = pre. next;
36
+ }
37
+
38
+ ListNode head2 = pre;
39
+ pre = pre. next;
40
+ ListNode cur = pre. next;
41
+ for (; i < n - 1 ; ++ i) {
42
+ pre. next = cur. next;
43
+ cur. next = head2. next;
44
+ head2. next = cur;
45
+ cur = pre. next;
46
+ }
47
+
48
+ return dummy. next;
49
+
50
+
51
+ }
52
+ }
53
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public ListNode reverseBetween (ListNode head , int m , int n ) {
3
+ ListNode dummy = new ListNode (-1 );
4
+ dummy .next = head ;
5
+ ListNode pre = dummy ;
6
+ int i = 0 ;
7
+ for (; i < m - 1 ; ++i ) {
8
+ pre = pre .next ;
9
+ }
10
+
11
+ ListNode head2 = pre ;
12
+ pre = pre .next ;
13
+ ListNode cur = pre .next ;
14
+ for (; i < n - 1 ; ++i ) {
15
+ pre .next = cur .next ;
16
+ cur .next = head2 .next ;
17
+ head2 .next = cur ;
18
+ cur = pre .next ;
19
+ }
20
+
21
+ return dummy .next ;
22
+
23
+
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments