forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
32 lines (32 loc) · 648 Bytes
/
Copy pathmain.js
File metadata and controls
32 lines (32 loc) · 648 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
/**
* 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) {
const dummy = new ListNode(-1)
dummy.next = head
const small = new ListNode(-1)
let fast = dummy
let slow = small
while (fast.next != null) {
if (fast.next.val < x) {
const moved = fast.next
fast.next = moved.next
moved.next = slow.next
slow.next = moved
slow = moved
} else {
fast = fast.next
}
}
slow.next = dummy.next
return small.next
};