Skip to content

Commit ef7f65a

Browse files
committed
Add solution #86
1 parent 7e8f7dc commit ef7f65a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
78|[Subsets](./0078-subsets.js)|Medium|
7575
80|[Remove Duplicates from Sorted Array II](./0080-remove-duplicates-from-sorted-array-ii.js)|Medium|
7676
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
77+
86|[Partition List](./0086-partition-list.js)|Medium|
7778
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
7879
90|[Subsets II](./0090-subsets-ii.js)|Medium|
7980
93|[Restore IP Addresses](./0093-restore-ip-addresses.js)|Medium|

solutions/0086-partition-list.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 86. Partition List
3+
* https://leetcode.com/problems/partition-list/
4+
* Difficulty: Medium
5+
*
6+
* Given the head of a linked list and a value x, partition it such that
7+
* all nodes less than x come before nodes greater than or equal to x.
8+
*
9+
* You should preserve the original relative order of the nodes in each
10+
* of the two partitions.
11+
*/
12+
13+
/**
14+
* @param {ListNode} head
15+
* @param {number} x
16+
* @return {ListNode}
17+
*/
18+
var partition = function(head, x) {
19+
const result = [];
20+
const stack = [];
21+
22+
while (head) {
23+
const target = head.val >= x ? result : stack;
24+
target.push(head.val);
25+
head = head.next;
26+
}
27+
28+
return [...stack, ...result].reverse().reduce((a, b) => {
29+
return new ListNode(b, a);
30+
}, null);
31+
};

0 commit comments

Comments
 (0)