File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode(int x) : val(x), next(NULL) {}
7
+ * };
8
+ */
9
+ class Solution {
10
+ public:
11
+ ListNode *partition (ListNode *head, int x) {
12
+ // Start typing your C/C++ solution below
13
+ // DO NOT write int main() function
14
+
15
+ ListNode less_than_head (0 );
16
+ ListNode no_less_than_head (0 );
17
+ ListNode *less_than = &less_than_head;
18
+ ListNode *no_less_than = &no_less_than_head;
19
+
20
+ ListNode *node = head;
21
+ while (node != NULL ) {
22
+ if (node->val < x) {
23
+ less_than->next = node;
24
+ node = node->next ;
25
+ less_than = less_than->next ;
26
+ }
27
+ else {
28
+ no_less_than->next = node;
29
+ node = node->next ;
30
+ no_less_than = no_less_than->next ;
31
+ }
32
+ }
33
+ no_less_than->next = NULL ;
34
+ less_than->next = no_less_than_head.next ;
35
+ return less_than_head.next ;
36
+ }
37
+ };
You can’t perform that action at this time.
0 commit comments