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