-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sorted_list.cpp
84 lines (72 loc) · 1.72 KB
/
merge_sorted_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
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int data = 0){
this->val = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted: "<<this->val<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}
// Iterative Apporach
Node* mergeTwoLists(Node* &list1, Node* &list2) {
if(list1 == NULL || list2 == NULL){
return list1 == NULL ? list2 : list1;
}
Node* ansHead = new Node(-1);
Node* ansTail = ansHead;
while(list1 && list2){
if(list1->val <= list2->val){
ansTail->next = list1;
ansTail = list1;
list1 = list1->next;
}
else{
ansTail->next = list2;
ansTail = list2;
list2 = list2->next;
}
}
if(list1)
ansTail->next = list1;
if(list2)
ansTail->next = list2;
Node* temp = ansHead;
ansHead = ansHead->next;
delete temp;
return ansHead;
}
int main()
{
Node* list1 = new Node;
Node* sec1 = new Node(2);
Node* third1 = new Node(3);
Node* forth1 = new Node(6);
list1->next = sec1;
sec1->next = third1;
third1->next = forth1;
Node* list2 = new Node(1);
Node* sec2 = new Node(3);
Node* third2 = new Node(4);
// Node* forth2 = new Node();
list2->next = sec2;
sec2->next = third2;
// third2->next = forth2;
Node* ansHead = mergeTwoLists(list1,list2);
print(ansHead);
return 0;
}