File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 747478|[ Subsets] ( ./0078-subsets.js ) |Medium|
757580|[ Remove Duplicates from Sorted Array II] ( ./0080-remove-duplicates-from-sorted-array-ii.js ) |Medium|
767683|[ Remove Duplicates from Sorted List] ( ./0083-remove-duplicates-from-sorted-list.js ) |Easy|
77+ 86|[ Partition List] ( ./0086-partition-list.js ) |Medium|
777888|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
787990|[ Subsets II] ( ./0090-subsets-ii.js ) |Medium|
798093|[ Restore IP Addresses] ( ./0093-restore-ip-addresses.js ) |Medium|
You can’t perform that action at this time.
0 commit comments