-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisuan.cpp
122 lines (106 loc) · 2.45 KB
/
lisuan.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
/******************************************************************************
* File: lisuan.cpp
*
* Author: yychi
* Created: 2022-08-10 22:44
* Description: merge two sorted link list, iteratively and recursively.
*****************************************************************************/
#include <stdio.h>
struct ListNode
{
ListNode(int _val): val(_val), next(nullptr) {}
int val;
ListNode* next;
void print()
{
printf("%d", val);
if (next)
{
printf("->");
next->print();
}
else
{
printf("\n");
}
}
};
ListNode* MergeLinkList(ListNode* a, ListNode* b)
{
// two base cases.
if (!a) return b;
if (!b) return a;
ListNode ret(0);
ret.next = a;
ListNode* pa = &ret;
for (; a && b; )
{
if (a->val <= b->val)
{
pa = a;
a = a->next;
}
else
{
ListNode* a_pre = pa->next;
ListNode* b_nex = b->next;
pa->next = b;
pa->next->next = a_pre;
pa = pa->next;
b = b_nex;
}
}
// append tail
if (!a && b) pa->next = b;
return ret.next;
}
/**
* Recursive way.
*/
ListNode* MergeLinkListRecursive(ListNode* a, ListNode* b)
{
if (!a) return b;
if (!b) return a;
if (a->val <= b->val)
{
a->next = MergeLinkListRecursive(a->next, b);
return a;
}
else
{
b->next = MergeLinkListRecursive(a, b->next);
return b;
}
}
// test
int main()
{
ListNode* a = new ListNode(3);
ListNode* pa = a;
pa->next = new ListNode(5);
pa = pa->next;
pa->next = new ListNode(19);
pa = pa->next;
pa->next = new ListNode(21);
pa = pa->next;
pa->next = new ListNode(30);
ListNode* b = new ListNode(1);
ListNode* pb = b;
pb->next = new ListNode(4);
pb = pb->next;
pb->next = new ListNode(6);
pb = pb->next;
pb->next = new ListNode(8);
pb = pb->next;
pb->next = new ListNode(10);
pb = pb->next;
pb->next = new ListNode(45);
// print a, b
a->print();
b->print();
printf("before call a=%p, b=%p, a.next=%p, b.next=%p\n", a, b, a->next, b->next);
MergeLinkList(a, b)->print();
printf("after call a=%p, b=%p, a.next=%p, b.next=%p\n", a, b, a->next, b->next);
// MergeLinkListRecursive(a, b)->print();
return 0;
}