-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path86.cpp
93 lines (89 loc) · 2.5 KB
/
86.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
__________________________________________________________________________________________________
8ms
/**
* 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 less(-1);
ListNode *pless = &less;
ListNode bigger(-1);
ListNode *pbigger = &bigger;
ListNode *ret = pless;
ListNode *temp_e = pbigger;
for (ListNode *curs = head; curs != nullptr; curs = curs->next) {
if (curs->val < x) {
pless->next = curs;
pless = curs;
}
else {
pbigger->next = curs;
pbigger = curs;
}
}
pless->next = temp_e->next;
pbigger->next = nullptr;
return ret->next;
}
};
__________________________________________________________________________________________________
8384 kb
/**
* 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* curNode = head;
ListNode* curSmallNode = NULL;
ListNode* curLargeNode = NULL;
ListNode* firstLargeNode = NULL;
while(curNode!=NULL)
{
if(curNode->val>=x)
{
if(!firstLargeNode)
{
firstLargeNode = curNode;
}
else
{
curLargeNode->next = curNode;
}
curLargeNode = curNode;
}
else
{
if(curSmallNode)
{
curSmallNode->next = curNode;
}
else
{
head = curNode;
}
curSmallNode = curNode;
if(curSmallNode->next == NULL && curLargeNode != NULL)
{
curLargeNode->next = NULL;
}
}
curNode = curNode->next;
}
if(curSmallNode)
curSmallNode->next = firstLargeNode;
return head;
}
};
__________________________________________________________________________________________________