Skip to content

Commit 8a98916

Browse files
authored
add cpp solution
1 parent 6dcf4b7 commit 8a98916

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
ListNode headSmaller(0), headBigger(0) ;
13+
ListNode *pSmaller = &headSmaller ;
14+
ListNode *pBigger = &headBigger ;
15+
ListNode *p = head ;
16+
while (nullptr != p)
17+
{
18+
if (p->val < x)
19+
{
20+
pSmaller->next = p ;
21+
pSmaller = p ;
22+
}
23+
else
24+
{
25+
pBigger->next = p ;
26+
pBigger = p ;
27+
}
28+
p = p->next ;
29+
}
30+
pBigger->next = nullptr ;
31+
pSmaller->next = headBigger.next ;
32+
return headSmaller.next ;
33+
}
34+
};

0 commit comments

Comments
 (0)