-
Notifications
You must be signed in to change notification settings - Fork 0
/
_086.cpp
38 lines (37 loc) · 903 Bytes
/
_086.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* head1 = new ListNode(0);
ListNode* head2 = new ListNode(0);
ListNode* cur1 = head1;
ListNode* cur2 = head2;
while(head){
if(head->val < x){
cur1->next = head;
cur1 = head;
head = head->next;
}
else{
cur2->next = head;
cur2 = head;
head = head->next;
cur2->next = NULL;
}
}
if(head1->next){
cur1->next = head2->next;
return head1->next;
}
else{
return head2->next;
}
}
};