Skip to content

Commit 45a2169

Browse files
committed
feat: add solutions to lc/lcci problems: Partition List
1 parent c304aae commit 45a2169

File tree

10 files changed

+406
-153
lines changed

10 files changed

+406
-153
lines changed

lcci/02.04.Partition List/README.md

+61-29
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,19 @@
3434

3535
class Solution:
3636
def partition(self, head: ListNode, x: int) -> ListNode:
37-
if head is None or head.next is None:
38-
return head
39-
left, right = ListNode(-1), ListNode(-1)
40-
p, q = left, right
37+
l1, l2 = ListNode(0), ListNode(0)
38+
cur1, cur2 = l1, l2
4139
while head:
42-
t = head.next
43-
head.next = None
4440
if head.val < x:
45-
p.next = head
46-
p = p.next
41+
cur1.next = head
42+
cur1 = cur1.next
4743
else:
48-
q.next = head
49-
q = q.next
50-
head = t
51-
p.next = right.next
52-
return left.next
44+
cur2.next = head
45+
cur2 = cur2.next
46+
head = head.next
47+
cur1.next = l2.next
48+
cur2.next = None
49+
return l1.next
5350
```
5451

5552
### **Java**
@@ -62,35 +59,70 @@ class Solution:
6259
* public class ListNode {
6360
* int val;
6461
* ListNode next;
65-
* ListNode(int x) { val = x; }
62+
* ListNode() {}
63+
* ListNode(int val) { this.val = val; }
64+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
6665
* }
6766
*/
6867
class Solution {
6968
public ListNode partition(ListNode head, int x) {
70-
if (head == null || head.next == null) {
71-
return head;
72-
}
73-
ListNode left = new ListNode(-1);
74-
ListNode right = new ListNode(-1);
75-
ListNode p = left, q = right;
69+
ListNode l1 = new ListNode(0);
70+
ListNode l2 = new ListNode(0);
71+
ListNode cur1 = l1, cur2 = l2;
7672
while (head != null) {
77-
ListNode t = head.next;
78-
head.next = null;
7973
if (head.val < x) {
80-
p.next = head;
81-
p = p.next;
74+
cur1.next = head;
75+
cur1 = cur1.next;
8276
} else {
83-
q.next = head;
84-
q = q.next;
77+
cur2.next = head;
78+
cur2 = cur2.next;
8579
}
86-
head = t;
80+
head = head.next;
8781
}
88-
p.next = right.next;
89-
return left.next;
82+
cur1.next = l2.next;
83+
cur2.next = null;
84+
return l1.next;
9085
}
9186
}
9287
```
9388

89+
### **C++**
90+
91+
```cpp
92+
/**
93+
* Definition for singly-linked list.
94+
* struct ListNode {
95+
* int val;
96+
* ListNode *next;
97+
* ListNode() : val(0), next(nullptr) {}
98+
* ListNode(int x) : val(x), next(nullptr) {}
99+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
100+
* };
101+
*/
102+
class Solution {
103+
public:
104+
ListNode* partition(ListNode* head, int x) {
105+
ListNode* l1 = new ListNode();
106+
ListNode* l2 = new ListNode();
107+
ListNode* cur1 = l1;
108+
ListNode* cur2 = l2;
109+
while (head != nullptr) {
110+
if (head->val < x) {
111+
cur1->next = head;
112+
cur1 = cur1->next;
113+
} else {
114+
cur2->next = head;
115+
cur2 = cur2->next;
116+
}
117+
head = head->next;
118+
}
119+
cur1->next = l2->next;
120+
cur2->next = nullptr;
121+
return l1->next;
122+
}
123+
};
124+
```
125+
94126
### **...**
95127
96128
```

lcci/02.04.Partition List/README_EN.md

+61-29
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,19 @@
3131

3232
class Solution:
3333
def partition(self, head: ListNode, x: int) -> ListNode:
34-
if head is None or head.next is None:
35-
return head
36-
left, right = ListNode(-1), ListNode(-1)
37-
p, q = left, right
34+
l1, l2 = ListNode(0), ListNode(0)
35+
cur1, cur2 = l1, l2
3836
while head:
39-
t = head.next
40-
head.next = None
4137
if head.val < x:
42-
p.next = head
43-
p = p.next
38+
cur1.next = head
39+
cur1 = cur1.next
4440
else:
45-
q.next = head
46-
q = q.next
47-
head = t
48-
p.next = right.next
49-
return left.next
41+
cur2.next = head
42+
cur2 = cur2.next
43+
head = head.next
44+
cur1.next = l2.next
45+
cur2.next = None
46+
return l1.next
5047
```
5148

5249
### **Java**
@@ -57,35 +54,70 @@ class Solution:
5754
* public class ListNode {
5855
* int val;
5956
* ListNode next;
60-
* ListNode(int x) { val = x; }
57+
* ListNode() {}
58+
* ListNode(int val) { this.val = val; }
59+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
6160
* }
6261
*/
6362
class Solution {
6463
public ListNode partition(ListNode head, int x) {
65-
if (head == null || head.next == null) {
66-
return head;
67-
}
68-
ListNode left = new ListNode(-1);
69-
ListNode right = new ListNode(-1);
70-
ListNode p = left, q = right;
64+
ListNode l1 = new ListNode(0);
65+
ListNode l2 = new ListNode(0);
66+
ListNode cur1 = l1, cur2 = l2;
7167
while (head != null) {
72-
ListNode t = head.next;
73-
head.next = null;
7468
if (head.val < x) {
75-
p.next = head;
76-
p = p.next;
69+
cur1.next = head;
70+
cur1 = cur1.next;
7771
} else {
78-
q.next = head;
79-
q = q.next;
72+
cur2.next = head;
73+
cur2 = cur2.next;
8074
}
81-
head = t;
75+
head = head.next;
8276
}
83-
p.next = right.next;
84-
return left.next;
77+
cur1.next = l2.next;
78+
cur2.next = null;
79+
return l1.next;
8580
}
8681
}
8782
```
8883

84+
### **C++**
85+
86+
```cpp
87+
/**
88+
* Definition for singly-linked list.
89+
* struct ListNode {
90+
* int val;
91+
* ListNode *next;
92+
* ListNode() : val(0), next(nullptr) {}
93+
* ListNode(int x) : val(x), next(nullptr) {}
94+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
95+
* };
96+
*/
97+
class Solution {
98+
public:
99+
ListNode* partition(ListNode* head, int x) {
100+
ListNode* l1 = new ListNode();
101+
ListNode* l2 = new ListNode();
102+
ListNode* cur1 = l1;
103+
ListNode* cur2 = l2;
104+
while (head != nullptr) {
105+
if (head->val < x) {
106+
cur1->next = head;
107+
cur1 = cur1->next;
108+
} else {
109+
cur2->next = head;
110+
cur2 = cur2->next;
111+
}
112+
head = head->next;
113+
}
114+
cur1->next = l2->next;
115+
cur2->next = nullptr;
116+
return l1->next;
117+
}
118+
};
119+
```
120+
89121
### **...**
90122
91123
```
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* partition(ListNode* head, int x) {
14+
ListNode* l1 = new ListNode();
15+
ListNode* l2 = new ListNode();
16+
ListNode* cur1 = l1;
17+
ListNode* cur2 = l2;
18+
while (head != nullptr) {
19+
if (head->val < x) {
20+
cur1->next = head;
21+
cur1 = cur1->next;
22+
} else {
23+
cur2->next = head;
24+
cur2 = cur2->next;
25+
}
26+
head = head->next;
27+
}
28+
cur1->next = l2->next;
29+
cur2->next = nullptr;
30+
return l1->next;
31+
}
32+
};

lcci/02.04.Partition List/Solution.java

+14-16
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public ListNode partition(ListNode head, int x) {
11-
if (head == null || head.next == null) {
12-
return head;
13-
}
14-
ListNode left = new ListNode(-1);
15-
ListNode right = new ListNode(-1);
16-
ListNode p = left, q = right;
13+
ListNode l1 = new ListNode(0);
14+
ListNode l2 = new ListNode(0);
15+
ListNode cur1 = l1, cur2 = l2;
1716
while (head != null) {
18-
ListNode t = head.next;
19-
head.next = null;
2017
if (head.val < x) {
21-
p.next = head;
22-
p = p.next;
18+
cur1.next = head;
19+
cur1 = cur1.next;
2320
} else {
24-
q.next = head;
25-
q = q.next;
21+
cur2.next = head;
22+
cur2 = cur2.next;
2623
}
27-
head = t;
24+
head = head.next;
2825
}
29-
p.next = right.next;
30-
return left.next;
26+
cur1.next = l2.next;
27+
cur2.next = null;
28+
return l1.next;
3129
}
3230
}

lcci/02.04.Partition List/Solution.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44
# self.val = x
55
# self.next = None
66

7-
87
class Solution:
98
def partition(self, head: ListNode, x: int) -> ListNode:
10-
if head is None or head.next is None:
11-
return head
12-
left, right = ListNode(-1), ListNode(-1)
13-
p, q = left, right
9+
l1, l2 = ListNode(0), ListNode(0)
10+
cur1, cur2 = l1, l2
1411
while head:
15-
t = head.next
16-
head.next = None
1712
if head.val < x:
18-
p.next = head
19-
p = p.next
13+
cur1.next = head
14+
cur1 = cur1.next
2015
else:
21-
q.next = head
22-
q = q.next
23-
head = t
24-
p.next = right.next
25-
return left.next
16+
cur2.next = head
17+
cur2 = cur2.next
18+
head = head.next
19+
cur1.next = l2.next
20+
cur2.next = None
21+
return l1.next

0 commit comments

Comments
 (0)