55
55
56
56
### 方法一:模拟
57
57
58
- 我们创建两个链表,一个存放小于 $x$ 的节点,另一个存放大于等于 $x$ 的节点,之后进行拼接即可 。
58
+ 我们创建两个链表 $l$ 和 $r$,一个用来存储小于 $x$ 的节点,另一个用来存储大于等于 $x$ 的节点。然后我们将它们拼接起来 。
59
59
60
- 时间复杂度 $O(n),其中 $n$ 是原链表的长度。空间复杂度 $O(1)$。
60
+ 时间复杂度 $O(n)$ ,其中 $n$ 是原链表的长度。空间复杂度 $O(1)$。
61
61
62
62
<!-- tabs:start -->
63
63
@@ -71,19 +71,20 @@ tags:
71
71
# self.next = next
72
72
class Solution :
73
73
def partition (self , head : Optional[ListNode], x : int ) -> Optional[ListNode]:
74
- d1, d2 = ListNode(), ListNode()
75
- t1, t2 = d1, d2
74
+ l = ListNode()
75
+ r = ListNode()
76
+ tl, tr = l, r
76
77
while head:
77
78
if head.val < x:
78
- t1 .next = head
79
- t1 = t1 .next
79
+ tl .next = head
80
+ tl = tl .next
80
81
else :
81
- t2 .next = head
82
- t2 = t2 .next
82
+ tr .next = head
83
+ tr = tr .next
83
84
head = head.next
84
- t1 .next = d2.next
85
- t2 .next = None
86
- return d1 .next
85
+ tr .next = None
86
+ tl .next = r.next
87
+ return l .next
87
88
```
88
89
89
90
#### Java
@@ -101,22 +102,21 @@ class Solution:
101
102
*/
102
103
class Solution {
103
104
public ListNode partition (ListNode head , int x ) {
104
- ListNode d1 = new ListNode ();
105
- ListNode d2 = new ListNode ();
106
- ListNode t1 = d1, t2 = d2 ;
107
- while ( head != null ) {
105
+ ListNode l = new ListNode ();
106
+ ListNode r = new ListNode ();
107
+ ListNode tl = l, tr = r ;
108
+ for (; head != null ; head = head . next ) {
108
109
if (head. val < x) {
109
- t1 . next = head;
110
- t1 = t1 . next;
110
+ tl . next = head;
111
+ tl = tl . next;
111
112
} else {
112
- t2 . next = head;
113
- t2 = t2 . next;
113
+ tr . next = head;
114
+ tr = tr . next;
114
115
}
115
- head = head. next;
116
116
}
117
- t1 . next = d2 . next ;
118
- t2 . next = null ;
119
- return d1 . next;
117
+ tr . next = null ;
118
+ tl . next = r . next ;
119
+ return l . next;
120
120
}
121
121
}
122
122
```
@@ -137,23 +137,22 @@ class Solution {
137
137
class Solution {
138
138
public:
139
139
ListNode* partition(ListNode* head, int x) {
140
- ListNode* d1 = new ListNode();
141
- ListNode* d2 = new ListNode();
142
- ListNode* t1 = d1 ;
143
- ListNode* t2 = d2 ;
144
- while ( head) {
140
+ ListNode* l = new ListNode();
141
+ ListNode* r = new ListNode();
142
+ ListNode* tl = l ;
143
+ ListNode* tr = r ;
144
+ for (; head; head = head->next ) {
145
145
if (head->val < x) {
146
- t1 ->next = head;
147
- t1 = t1 ->next;
146
+ tl ->next = head;
147
+ tl = tl ->next;
148
148
} else {
149
- t2 ->next = head;
150
- t2 = t2 ->next;
149
+ tr ->next = head;
150
+ tr = tr ->next;
151
151
}
152
- head = head->next;
153
152
}
154
- t1 ->next = d2->next ;
155
- t2 ->next = nullptr ;
156
- return d1 ->next;
153
+ tr ->next = nullptr ;
154
+ tl ->next = r->next ;
155
+ return l ->next;
157
156
}
158
157
};
159
158
```
@@ -169,21 +168,53 @@ public:
169
168
* }
170
169
*/
171
170
func partition(head *ListNode, x int) *ListNode {
172
- d1, d2 := &ListNode{}, &ListNode{}
173
- t1, t2 := d1, d2
174
- for head != nil {
171
+ l, r := &ListNode{}, &ListNode{}
172
+ tl, tr := l, r
173
+ for ; head != nil; head = head.Next {
175
174
if head.Val < x {
176
- t1 .Next = head
177
- t1 = t1 .Next
175
+ tl .Next = head
176
+ tl = tl .Next
178
177
} else {
179
- t2 .Next = head
180
- t2 = t2 .Next
178
+ tr .Next = head
179
+ tr = tr .Next
181
180
}
182
- head = head.Next
183
181
}
184
- t1.Next = d2.Next
185
- t2.Next = nil
186
- return d1.Next
182
+ tr.Next = nil
183
+ tl.Next = r.Next
184
+ return l.Next
185
+ }
186
+ ```
187
+
188
+ #### TypeScript
189
+
190
+ ``` ts
191
+ /**
192
+ * Definition for singly-linked list.
193
+ * class ListNode {
194
+ * val: number
195
+ * next: ListNode | null
196
+ * constructor(val?: number, next?: ListNode | null) {
197
+ * this.val = (val===undefined ? 0 : val)
198
+ * this.next = (next===undefined ? null : next)
199
+ * }
200
+ * }
201
+ */
202
+
203
+ function partition(head : ListNode | null , x : number ): ListNode | null {
204
+ const [l, r] = [new ListNode (), new ListNode ()];
205
+ let [tl, tr] = [l , r ];
206
+ for (; head ; head = head .next ) {
207
+ if (head .val < x ) {
208
+ tl .next = head ;
209
+ tl = tl .next ;
210
+ } else {
211
+ tr .next = head ;
212
+ tr = tr .next ;
213
+ }
214
+ }
215
+ tr .next = null ;
216
+ tl .next = r .next ;
217
+ return l .next ;
187
218
}
188
219
```
189
220
@@ -208,22 +239,24 @@ func partition(head *ListNode, x int) *ListNode {
208
239
// }
209
240
impl Solution {
210
241
pub fn partition (head : Option <Box <ListNode >>, x : i32 ) -> Option <Box <ListNode >> {
211
- let mut head = head ;
212
- let mut d1 = Some (Box :: new (ListNode :: new (0 )));
213
- let mut d2 = Some (Box :: new (ListNode :: new (0 )));
214
- let (mut t1 , mut t2 ) = (& mut d1 , & mut d2 );
215
- while let Some (mut node ) = head {
216
- head = node . next. take ();
242
+ let mut l = ListNode :: new (0 );
243
+ let mut r = ListNode :: new (0 );
244
+ let mut tl = & mut l ;
245
+ let mut tr = & mut r ;
246
+ let mut current = head ;
247
+ while let Some (mut node ) = current {
248
+ current = node . next. take ();
217
249
if node . val < x {
218
- t1 . as_mut () . unwrap () . next = Some (node );
219
- t1 = & mut t1 . as_mut (). unwrap (). next ;
250
+ tl . next = Some (node );
251
+ tl = tl . next . as_mut (). unwrap ();
220
252
} else {
221
- t2 . as_mut () . unwrap () . next = Some (node );
222
- t2 = & mut t2 . as_mut (). unwrap (). next ;
253
+ tr . next = Some (node );
254
+ tr = tr . next . as_mut (). unwrap ();
223
255
}
224
256
}
225
- t1 . as_mut (). unwrap (). next = d2 . unwrap (). next;
226
- d1 . unwrap (). next
257
+ tr . next = None ;
258
+ tl . next = r . next;
259
+ l . next
227
260
}
228
261
}
229
262
```
@@ -244,26 +277,58 @@ impl Solution {
244
277
* @return {ListNode}
245
278
*/
246
279
var partition = function (head , x ) {
247
- const d1 = new ListNode ();
248
- const d2 = new ListNode ();
249
- let t1 = d1,
250
- t2 = d2;
251
- while (head) {
280
+ const [l , r ] = [new ListNode (), new ListNode ()];
281
+ let [tl, tr] = [l, r];
282
+ for (; head; head = head .next ) {
252
283
if (head .val < x) {
253
- t1 .next = head;
254
- t1 = t1 .next ;
284
+ tl .next = head;
285
+ tl = tl .next ;
255
286
} else {
256
- t2 .next = head;
257
- t2 = t2 .next ;
287
+ tr .next = head;
288
+ tr = tr .next ;
258
289
}
259
- head = head .next ;
260
290
}
261
- t1 .next = d2 . next ;
262
- t2 .next = null ;
263
- return d1 .next ;
291
+ tr .next = null ;
292
+ tl .next = r . next ;
293
+ return l .next ;
264
294
};
265
295
```
266
296
297
+ #### C#
298
+
299
+ ``` cs
300
+ /**
301
+ * Definition for singly-linked list.
302
+ * public class ListNode {
303
+ * public int val;
304
+ * public ListNode next;
305
+ * public ListNode(int val=0, ListNode next=null) {
306
+ * this.val = val;
307
+ * this.next = next;
308
+ * }
309
+ * }
310
+ */
311
+ public class Solution {
312
+ public ListNode Partition (ListNode head , int x ) {
313
+ ListNode l = new ListNode ();
314
+ ListNode r = new ListNode ();
315
+ ListNode tl = l , tr = r ;
316
+ for (; head != null ; head = head .next ) {
317
+ if (head .val < x ) {
318
+ tl .next = head ;
319
+ tl = tl .next ;
320
+ } else {
321
+ tr .next = head ;
322
+ tr = tr .next ;
323
+ }
324
+ }
325
+ tr .next = null ;
326
+ tl .next = r .next ;
327
+ return l .next ;
328
+ }
329
+ }
330
+ ```
331
+
267
332
<!-- tabs: end -->
268
333
269
334
<!-- solution: end -->
0 commit comments