File tree 3 files changed +78
-0
lines changed
solution/086.Partition List
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ 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 ` |
53
54
| 092 | [ Reverse Linked List II] ( https://github.com/yanglbme/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II ) | ` Linked List ` |
54
55
| 094 | [ Binary Tree Inorder Traversal] ( https://github.com/yanglbme/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal ) | ` Hash Table ` , ` Stack ` , ` Tree ` |
55
56
| 144 | [ Binary Tree Preorder Traversal] ( https://github.com/yanglbme/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal ) | ` Stack ` , ` Tree ` |
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
+ }
You can’t perform that action at this time.
0 commit comments