Skip to content

Commit 6c8548f

Browse files
committed
feat: add solutions to lc problem: No.0086
No.0086.Partition List
1 parent 8b24426 commit 6c8548f

File tree

7 files changed

+239
-157
lines changed

7 files changed

+239
-157
lines changed

solution/0000-0099/0086.Partition List/README.md

+82-54
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@
5656
# self.next = next
5757
class Solution:
5858
def partition(self, head: ListNode, x: int) -> ListNode:
59-
l1, l2 = ListNode(), ListNode()
60-
cur1, cur2 = l1, l2
59+
d1, d2 = ListNode(), ListNode()
60+
t1, t2 = d1, d2
6161
while head:
6262
if head.val < x:
63-
cur1.next = head
64-
cur1 = cur1.next
63+
t1.next = head
64+
t1 = t1.next
6565
else:
66-
cur2.next = head
67-
cur2 = cur2.next
66+
t2.next = head
67+
t2 = t2.next
6868
head = head.next
69-
cur1.next = l2.next
70-
cur2.next = None
71-
return l1.next
69+
t1.next = d2.next
70+
t2.next = None
71+
return d1.next
7272
```
7373

7474
### **Java**
@@ -88,22 +88,22 @@ class Solution:
8888
*/
8989
class Solution {
9090
public ListNode partition(ListNode head, int x) {
91-
ListNode l1 = new ListNode(0);
92-
ListNode l2 = new ListNode(0);
93-
ListNode cur1 = l1, cur2 = l2;
91+
ListNode d1 = new ListNode();
92+
ListNode d2 = new ListNode();
93+
ListNode t1 = d1, t2 = d2;
9494
while (head != null) {
9595
if (head.val < x) {
96-
cur1.next = head;
97-
cur1 = cur1.next;
96+
t1.next = head;
97+
t1 = t1.next;
9898
} else {
99-
cur2.next = head;
100-
cur2 = cur2.next;
99+
t2.next = head;
100+
t2 = t2.next;
101101
}
102102
head = head.next;
103103
}
104-
cur1.next = l2.next;
105-
cur2.next = null;
106-
return l1.next;
104+
t1.next = d2.next;
105+
t2.next = null;
106+
return d1.next;
107107
}
108108
}
109109
```
@@ -124,26 +124,60 @@ class Solution {
124124
class Solution {
125125
public:
126126
ListNode* partition(ListNode* head, int x) {
127-
ListNode* l1 = new ListNode();
128-
ListNode* l2 = new ListNode();
129-
ListNode* cur1 = l1;
130-
ListNode* cur2 = l2;
131-
while (head != nullptr) {
132-
if (head->val < x) {
133-
cur1->next = head;
134-
cur1 = cur1->next;
135-
} else {
136-
cur2->next = head;
137-
cur2 = cur2->next;
127+
ListNode* d1 = new ListNode();
128+
ListNode* d2 = new ListNode();
129+
ListNode* t1 = d1;
130+
ListNode* t2 = d2;
131+
while (head)
132+
{
133+
if (head->val < x)
134+
{
135+
t1->next = head;
136+
t1 = t1->next;
137+
}
138+
else
139+
{
140+
t2->next = head;
141+
t2 = t2->next;
138142
}
139143
head = head->next;
140144
}
141-
cur1->next = l2->next;
142-
cur2->next = nullptr;
143-
return l1->next;
145+
t1->next = d2->next;
146+
t2->next = nullptr;
147+
return d1->next;
144148
}
145149
};
146150
```
151+
152+
### **Go**
153+
154+
```go
155+
/**
156+
* Definition for singly-linked list.
157+
* type ListNode struct {
158+
* Val int
159+
* Next *ListNode
160+
* }
161+
*/
162+
func partition(head *ListNode, x int) *ListNode {
163+
d1, d2 := &ListNode{}, &ListNode{}
164+
t1, t2 := d1, d2
165+
for head != nil {
166+
if head.Val < x {
167+
t1.Next = head
168+
t1 = t1.Next
169+
} else {
170+
t2.Next = head
171+
t2 = t2.Next
172+
}
173+
head = head.Next
174+
}
175+
t1.Next = d2.Next
176+
t2.Next = nil
177+
return d1.Next
178+
}
179+
```
180+
147181
### **JavaScript**
148182

149183
```js
@@ -156,32 +190,26 @@ public:
156190
*/
157191
/**
158192
* @param {ListNode} head
193+
* @param {number} x
159194
* @return {ListNode}
160195
*/
161-
var deleteDuplicates = function (head) {
162-
let cur = head;
163-
let pre = new ListNode(0);
164-
pre.next = head;
165-
let dummy = pre;
166-
let rep = false;
167-
if (!head || !head.next) {
168-
return head;
169-
}
170-
while (cur) {
171-
while (cur.next && cur.val == cur.next.val) {
172-
cur = cur.next;
173-
rep = true;
174-
}
175-
if (rep) {
176-
pre.next = cur.next;
177-
cur = cur.next;
196+
var partition = function(head, x) {
197+
const d1 = new ListNode();
198+
const d2 = new ListNode();
199+
let t1 = d1, t2 = d2;
200+
while (head) {
201+
if (head.val < x) {
202+
t1.next = head;
203+
t1 = t1.next;
178204
} else {
179-
pre = cur;
180-
cur = cur.next;
205+
t2.next = head;
206+
t2 = t2.next;
181207
}
182-
rep = false;
208+
head = head.next;
183209
}
184-
return dummy.next;
210+
t1.next = d2.next;
211+
t2.next = null;
212+
return d1.next;
185213
};
186214
```
187215

solution/0000-0099/0086.Partition List/README_EN.md

+81-51
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@
4646
# self.next = next
4747
class Solution:
4848
def partition(self, head: ListNode, x: int) -> ListNode:
49-
l1, l2 = ListNode(), ListNode()
50-
cur1, cur2 = l1, l2
49+
d1, d2 = ListNode(), ListNode()
50+
t1, t2 = d1, d2
5151
while head:
5252
if head.val < x:
53-
cur1.next = head
54-
cur1 = cur1.next
53+
t1.next = head
54+
t1 = t1.next
5555
else:
56-
cur2.next = head
57-
cur2 = cur2.next
56+
t2.next = head
57+
t2 = t2.next
5858
head = head.next
59-
cur1.next = l2.next
60-
cur2.next = None
61-
return l1.next
59+
t1.next = d2.next
60+
t2.next = None
61+
return d1.next
6262
```
6363

6464
### **Java**
@@ -76,22 +76,22 @@ class Solution:
7676
*/
7777
class Solution {
7878
public ListNode partition(ListNode head, int x) {
79-
ListNode l1 = new ListNode(0);
80-
ListNode l2 = new ListNode(0);
81-
ListNode cur1 = l1, cur2 = l2;
79+
ListNode d1 = new ListNode();
80+
ListNode d2 = new ListNode();
81+
ListNode t1 = d1, t2 = d2;
8282
while (head != null) {
8383
if (head.val < x) {
84-
cur1.next = head;
85-
cur1 = cur1.next;
84+
t1.next = head;
85+
t1 = t1.next;
8686
} else {
87-
cur2.next = head;
88-
cur2 = cur2.next;
87+
t2.next = head;
88+
t2 = t2.next;
8989
}
9090
head = head.next;
9191
}
92-
cur1.next = l2.next;
93-
cur2.next = null;
94-
return l1.next;
92+
t1.next = d2.next;
93+
t2.next = null;
94+
return d1.next;
9595
}
9696
}
9797
```
@@ -112,26 +112,60 @@ class Solution {
112112
class Solution {
113113
public:
114114
ListNode* partition(ListNode* head, int x) {
115-
ListNode* l1 = new ListNode();
116-
ListNode* l2 = new ListNode();
117-
ListNode* cur1 = l1;
118-
ListNode* cur2 = l2;
119-
while (head != nullptr) {
120-
if (head->val < x) {
121-
cur1->next = head;
122-
cur1 = cur1->next;
123-
} else {
124-
cur2->next = head;
125-
cur2 = cur2->next;
115+
ListNode* d1 = new ListNode();
116+
ListNode* d2 = new ListNode();
117+
ListNode* t1 = d1;
118+
ListNode* t2 = d2;
119+
while (head)
120+
{
121+
if (head->val < x)
122+
{
123+
t1->next = head;
124+
t1 = t1->next;
125+
}
126+
else
127+
{
128+
t2->next = head;
129+
t2 = t2->next;
126130
}
127131
head = head->next;
128132
}
129-
cur1->next = l2->next;
130-
cur2->next = nullptr;
131-
return l1->next;
133+
t1->next = d2->next;
134+
t2->next = nullptr;
135+
return d1->next;
132136
}
133137
};
134138
```
139+
140+
### **Go**
141+
142+
```go
143+
/**
144+
* Definition for singly-linked list.
145+
* type ListNode struct {
146+
* Val int
147+
* Next *ListNode
148+
* }
149+
*/
150+
func partition(head *ListNode, x int) *ListNode {
151+
d1, d2 := &ListNode{}, &ListNode{}
152+
t1, t2 := d1, d2
153+
for head != nil {
154+
if head.Val < x {
155+
t1.Next = head
156+
t1 = t1.Next
157+
} else {
158+
t2.Next = head
159+
t2 = t2.Next
160+
}
161+
head = head.Next
162+
}
163+
t1.Next = d2.Next
164+
t2.Next = nil
165+
return d1.Next
166+
}
167+
```
168+
135169
### **JavaScript**
136170

137171
```js
@@ -147,27 +181,23 @@ public:
147181
* @param {number} x
148182
* @return {ListNode}
149183
*/
150-
var partition = function (head, x) {
151-
152-
if (!head || !head.next) {
153-
return head;
154-
}
155-
const dummy1 = new ListNode(-1), dummy2 = new ListNode(-1);
156-
let cur1 = dummy1, cur2 = dummy2;
157-
let cur = head;
158-
while (cur) {
159-
if (cur.val < x) {
160-
cur1.next = cur;
161-
cur1 = cur1.next;
184+
var partition = function(head, x) {
185+
const d1 = new ListNode();
186+
const d2 = new ListNode();
187+
let t1 = d1, t2 = d2;
188+
while (head) {
189+
if (head.val < x) {
190+
t1.next = head;
191+
t1 = t1.next;
162192
} else {
163-
cur2.next = cur;
164-
cur2 = cur2.next;
193+
t2.next = head;
194+
t2 = t2.next;
165195
}
166-
cur = cur.next;
196+
head = head.next;
167197
}
168-
cur2.next = null;
169-
cur1.next = dummy2.next;
170-
return (dummy1.next);
198+
t1.next = d2.next;
199+
t2.next = null;
200+
return d1.next;
171201
};
172202
```
173203

0 commit comments

Comments
 (0)