-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPartition List.cpp
43 lines (43 loc) · 985 Bytes
/
Partition List.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
39
40
41
42
43
/**
* 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 *h=NULL,*ptr=NULL,*p=head;
ListNode *h1=NULL,*ptr1=NULL;
while (p)
{
if (p->val<x)
{
if (!h)h=ptr=p;
else
{
ptr->next=p;
ptr=p;
}
}
else
{
if (!h1)h1=ptr1=p;
else
{
ptr1->next=p;
ptr1=p;
}
}
p=p->next;
}
if (ptr1)ptr1->next=NULL;
if (ptr)ptr->next=h1;
else h=h1;
return h;
}
};