-
Notifications
You must be signed in to change notification settings - Fork 0
/
L49-1.cpp
73 lines (50 loc) · 1.41 KB
/
L49-1.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
// Merge two linked lists
/************************************************************
Following is the linked list node structure.
template <typename T>
class Node {
public:
T data;
Node* next;
Node(T data) {
next = NULL;
this->data = data;
}
~Node() {
if (next != NULL) {
delete next;
}
}
};
************************************************************/
void solve(Node<int>* first, Node<int>* second) {
Node* curr1 = first;
Node* next1 = curr1 -> next;
Node* curr2 = second;
Node* next2 = curr2 -> next;
while(next1 != NULL && curr2 != NULL) {
if( (curr2 -> data >= curr1 -> data )
&& ( curr2 -> data <= next1 -> data)) {
curr1 -> next = curr2;
curr2 -> next = next1;
curr1 = curr2;
curr2 = next2;
}
else {
}
}
}
Node<int>* sortTwoLists(Node<int>* first, Node<int>* second)
{
if(first == NULL)
return second;
if(second == NULL)
return first;
if(first -> data <= second -> data ){
solve(first, second);
}
else
{
solve(second, first);
}
}