-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path86.分隔链表-01.js
43 lines (42 loc) · 863 Bytes
/
86.分隔链表-01.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* @lc app=leetcode.cn id=86 lang=javascript
*
* [86] 分隔链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function (head, x) {
let smallHead = new ListNode(0, null);
let bigHead = new ListNode(0, null);
let smallTail = smallHead;
let bigTail = bigHead;
let p = head;
let q;
while (p) {
q = p.next;
if (p.val < x) {
p.next = smallTail.next;
smallTail.next = p;
smallTail = p;
} else {
p.next = bigTail.next;
bigTail.next = p;
bigTail = p;
}
p = q;
}
smallTail.next = bigHead.next;
return smallHead.next;
};
// @lc code=end