-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path分隔链表.cpp
78 lines (69 loc) · 1.49 KB
/
分隔链表.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
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
/*
思路不太难,但很讲究基本功
首先找到值比x大的前一个节点,作为插入的节点
然后找到所有值比x小的节点,更改next指向。
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode * partition(ListNode* head, int x)
{
if (!head)
return nullptr;
ListNode fakeHead(0);
fakeHead.next = head;
ListNode *p_insert = &fakeHead;
//找到第一个大于等于x的节点的前一个节点,作为以后要插入的节点
while (p_insert->next && p_insert->next->val < x)
p_insert = p_insert->next;
ListNode *p = p_insert->next;
//接下来是插入操作
while (p&&p->next)
{
//跳过值比x大的节点
while (p->next && p->next->val >= x)
p = p->next;
if (!p->next)
break;
//更改节点的next
ListNode *temp = p->next;
p->next = temp->next;
temp->next = p_insert->next;
p_insert->next = temp;
p_insert = p_insert->next;
}
return fakeHead.next;
}
};
int main()
{
ListNode node1(3);
ListNode node2(3);
ListNode node3(1);
ListNode node4(2);
ListNode node5(5);
ListNode node6(2);
node1.next = &node2;
//node2.next = &node3;
//node3.next = &node4;
//node4.next = &node5;
//node5.next = &node6;
Solution a;
ListNode *p = a.partition(&node1, 4);
while (p)
{
cout << p->val << " ";
p = p->next;
}
cout << endl;
system("pause");
}