-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path合并两个有序链表.cpp
101 lines (87 loc) · 1.51 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
if (l1 == nullptr & l2 == nullptr)
return nullptr;
if (l1 == nullptr)
return l2;
if (l2 == nullptr)
return l1;
auto *p1 = l1, *p2 = l2;
auto *res = new ListNode{ 0 };
auto *k = res;
while (p1 != nullptr & p2 != nullptr)
{
if (p1->val > p2->val)
{
k->next = new ListNode{ p2->val };
p2 = p2->next;
}
else
{
k->next = new ListNode{ p1->val };
p1 = p1->next;
}
k = k->next;
}
if (p1 == nullptr)
k->next = p2;
else
k->next = p1;
return res->next;
}
//2019.10.7
ListNode * mergeTwoLists(ListNode* l1, ListNode* l2)
{
ListNode *head = new ListNode(-1);
ListNode *p = head;
while (l1&&l2)
{
if (l1->val <= l2->val)
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return head->next;
}
};
int main()
{
ListNode node1{ 1 };
ListNode node2{ 2 };
ListNode node3{ 4 };
ListNode node4{ 1 };
ListNode node5{ 3 };
ListNode node6{ 4 };
node1.next = &node2;
node2.next = &node3;
node4.next = &node5;
node5.next = &node6;
Solution a;
auto *p = a.mergeTwoLists(&node1, &node4);
while (p != nullptr)
{
cout << p->val << endl;
p = p->next;
}
system("pause");
}