-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_list.cpp
77 lines (68 loc) · 1.95 KB
/
merge_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
#include <iostream>
using namespace std;
/**
* 题目:输入两个单调递增的链表,输出两个链表合成后的链表,
* 当然我们需要合成后的链表满足单调不减规则。
* 考点:链表
* 分析:对两个链表排序,我们可以新建一个链表用于接收合并后的数据
*/
/**
* 解法一:非递归
* temp 用来移动新链表,而temp会随着合并next会一直移动最后,所以再建一个
* root 用于返回新链表的头
*/
struct ListNode {
int val;
struct ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* temp = new ListNode(-1); // 游标节点
ListNode* root = temp; // 待返回的头节点
while (pHead1 && pHead2) {
if (pHead1->val < pHead2->val) {
temp->next = pHead1;
pHead1 = pHead1->next;
} else {
temp->next = pHead2;
pHead2 = pHead2->next;
}
temp = temp->next;
}
if (pHead1 == NULL) {
temp->next = pHead2;
}
if (pHead2 == NULL) {
temp->next = pHead1;
}
return root->next;
}
};
int main() {
int n;
Solution s = Solution();
ListNode* pNode = new ListNode(1);
pNode->next = NULL;
ListNode* pHead1 = pNode;
for (int i = 3; i <= 10; i = i + 2) {
ListNode* pTemp = new ListNode(i);
pNode->next = pTemp;
pNode = pTemp;
}
ListNode* pNode2 = new ListNode(2);
pNode2->next = NULL;
ListNode* pHead2 = pNode2;
for (int i = 4; i <= 10; i = i + 2) {
ListNode* pTemp = new ListNode(i);
pNode2->next = pTemp;
pNode2 = pTemp;
}
ListNode* head = s.Merge(pHead1, pHead2);
while (pHead1) {
cout << pHead1->val << endl;
pHead1 = pHead1->next;
}
return 0;
}