-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path86-Partition-List.js
More file actions
40 lines (34 loc) · 866 Bytes
/
Copy path86-Partition-List.js
File metadata and controls
40 lines (34 loc) · 866 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
36
37
38
39
40
/* eslint-disable no-undef */
/**
* 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}
*/
const partition = (head, x) => {
const frontHead = new ListNode();
const backHead = new ListNode();
let frontTail = frontHead;
let backTail = backHead;
let curr = head;
while (curr !== null) {
const next = curr.next;
if (curr.val < x) {
frontTail.next = curr;
frontTail = frontTail.next;
} else {
backTail.next = curr;
backTail = backTail.next;
}
curr = next;
}
frontTail.next = backHead.next;
backTail.next = null;
return frontHead.next;
};