We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6dcf4b7 commit 8a98916Copy full SHA for 8a98916
solution/0086.Partition List/Solution.cpp
@@ -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