-
Notifications
You must be signed in to change notification settings - Fork 12
/
86_Partition List.cpp
153 lines (128 loc) · 2.29 KB
/
86_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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <cstdlib>
using namespace std;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct ListNode{
int val;
ListNode *next;
ListNode(int x) :val(x), next(NULL){}
};
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (!head || !head->next)
return head;
ListNode *p = head;
head = NULL;
ListNode *r = head;
//(1)寻找第一个不小于目标值的节点p,前n个小于x的节点逐个连接到head,保存尾结点r
while (p && p->val < x)
{
if (!head)
{
head = p;
r = head;
}
else{
r->next = p;
//保存新链表尾结点
r = r->next;
}
p = p->next;
r->next = NULL;
}//while
//如果此时p为空,已经没有其余节点,返回新链表head
if (!p)
return head;
//(2)p节点大于x,从下一个结点开始遍历,将小于x的结点连接到head中,原链表删除该结点
ListNode *pre = p , *q = p->next;
while (q)
{
if (q->val < x)
{
if (!head)
{
head = q;
r = head;
}
else{
r->next = q;
//保存新链表尾结点
r = r->next;
}
pre->next = q->next;
q = q->next;
r->next = NULL;
}//if
else{
pre = q;
q = q->next;
}//else
}//while
//如果此时,原链表还剩余结点,直接连接到r后面即可
if (p)
{
if (!head)
return p;
else
r->next = p;
}
return head;
}
};
ListNode * insert(ListNode *head, int val)
{
ListNode *p = new ListNode(val);
if (head == NULL)
{
head = p;
}
else{
ListNode *q = head;
while (q->next != NULL)
{
q = q->next;
}
q->next = p;
}
return head;
}
void display(ListNode *head)
{
if (head == NULL)
return;
else if (head->next == NULL)
cout << head->val << endl;
else{
ListNode *q = head;
while (q->next)
{
cout << q->val << " -> ";
q = q->next;
}
cout << q->val << endl;
}
}
int main()
{
Solution s;
ListNode *head = NULL;
head = insert(head, 1);
head = insert(head, 4);
head = insert(head, 3);
head = insert(head, 2);
head = insert(head, 5);
head = insert(head, 2);
display(head);
head = s.partition(head, 0);
display(head);
system("pause");
return 0;
}