-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#70.cc
47 lines (42 loc) · 1.15 KB
/
LeetCode#70.cc
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
39
40
41
42
43
44
45
46
47
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *head1 =NULL ,*tail1=NULL, *head2=NULL, *tail2=NULL;
if(head==NULL) return NULL;
while(head){
if(head->val < x){
if(head1==NULL){
head1 = tail1 = head;
}
else{
tail1->next = head;
tail1 = head;
}
}
else{
if(head2==NULL){
head2 = tail2 = head;
}
else{
tail2->next = head;
tail2 = head;
}
}
head = head->next;
}
if(head1==NULL) head1 = head2;
else tail1->next = head2;
if(head2!=NULL) tail2->next = NULL;
return head1;
}
};