-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-solved.cpp
More file actions
82 lines (78 loc) · 1.75 KB
/
Copy pathmerge-solved.cpp
File metadata and controls
82 lines (78 loc) · 1.75 KB
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
/* TODO: Write a function that takes two pointers to linked lists
* which you can assume are in SORTED ORDER already. The function
* should then build a new list which is also in sorted order and
* return it.
* Bonus problem: re-write your function (or make a new version)
* which does NOT allocate any memory, and instead just re-uses the
* nodes from the two input lists. */
#include <iostream>
using std::cout;
using std::cin;
#include "list-utils.h"
node* merge(node* L1, node* L2)
{
node F; /* fake node to remove special cases: */
node* p = &F; /* p tells us where to attach next node */
while (L1 && L2) {
if (L1->data < L2->data) {
p->next = new node(L1->data);
L1 = L1->next;
} else {
p->next = new node(L2->data);
L2 = L2->next;
}
p = p->next;
}
while (L1) {
p->next = new node(L1->data);
L1 = L1->next;
p = p->next;
}
while (L2) {
p->next = new node(L2->data);
L2 = L2->next;
p = p->next;
}
return F.next;
}
/* bonus version, which is destructive of the original lists: */
node* merge2(node* L1, node* L2)
{
/* in case one list is empty, return the other: */
if (!L1) return L2;
if (!L2) return L1;
node* L;
if (L1->data < L2->data) {
L = L1;
L1 = L1->next;
} else {
L = L2;
L2 = L2->next;
}
node* p = L;
while (L1 && L2) {
if (L1->data < L2->data) {
p->next = L1;
L1 = L1->next;
} else {
p->next = L2;
L2 = L2->next;
}
p = p->next;
}
p->next = (node*)(reinterpret_cast<size_t>(L1) ^ reinterpret_cast<size_t>(L2));
return L;
}
int main()
{
/* some test code for your function: */
node* L1 = buildlist({1,5,7,11,13});
node* L2 = buildlist({2,3,4,8});
printlist(L1);
printlist(L2);
// node* L = merge(L1,L2);
node* L = merge2(L1,L2);
printlist(L);
return 0;
}
// vim:foldlevel=2