MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / partition

Function partition

linkedLists/partition.js:13–44  ·  view source on GitHub ↗
(list, val)

Source from the content-addressed store, hash-verified

11 * list is being manipulated.
12 */
13export function partition(list, val) {
14 let node = list,
15 smallerHead,
16 smallerTail,
17 largerHead,
18 largerTail;
19
20 smallerHead = smallerTail = largerHead = largerTail = null;
21 while (node) {
22 let next = node.next;
23 node.next = null;
24 if (node.val >= val) {
25 if (!largerTail) {
26 largerHead = largerTail = node;
27 } else {
28 largerTail = largerTail.next = node;
29 }
30 } else if (node.val < val) {
31 if (!smallerHead) {
32 smallerHead = smallerTail = node;
33 } else {
34 smallerTail = smallerTail.next = node;
35 }
36 }
37 node = next;
38 }
39
40 if (smallerTail) {
41 smallerTail.next = largerHead;
42 }
43 return smallerHead || largerHead;
44}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected