-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
35 lines (32 loc) · 747 Bytes
/
solution.js
File metadata and controls
35 lines (32 loc) · 747 Bytes
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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
let dummySmaller = new ListNode(-1),
dummyBigger = new ListNode(-1),
pSmaller = dummySmaller,
pBigger = dummyBigger
while (head) {
let p = head
head = head.next
p.next = null
if (p.val < x) {
pSmaller.next = p
pSmaller = pSmaller.next
} else {
pBigger.next = p
pBigger = pBigger.next
}
}
pSmaller.next = dummyBigger.next
return dummySmaller.next
};