Skip to content

Commit e2e6158

Browse files
committed
2
1 parent 9a9c47c commit e2e6158

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

PartitionList/PartitionList.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};

0 commit comments

Comments
 (0)