-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMerge Sorted Linked List.cpp
66 lines (58 loc) · 1.25 KB
/
Merge Sorted Linked 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
/* Hacker Blocks */
/* Title - Merge Sorted Linked List */
/* Created By - Akash Modak */
/* Date - 2/8/2020 */
// Given 2 sorted linked lists , merge the two given sorted linked list and print the final Linked List.
// Input Format
// First Line contains T the number of test cases.
// For each test case :
// Line 1 : N1 the size of list 1
// Line 2 : N1 elements for list 1
// Line 3 : N2 the size of list 2
// Line 4 : N2 elements for list 2
// Constraints
// 1 <= T <= 1000
// 0<= N1, N2 <= 10^6
// -10^7 <= Ai <= 10^7
// Output Format
// For each testcase , print the node data of merged linked list.
// Sample Input
// 1
// 4
// 1 3 5 7
// 3
// 2 4 6
// Sample Output
// 1 2 3 4 5 6 7
// Explanation
// The two lists after merging give the sorted output as [1,2,3,4,5,6,7] .
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n1,n2;
list<int> l1,l2;
cin>>n1;
for(int i=0;i<n1;i++)
{
int temp;
cin>>temp;
l1.push_back(temp);
}
cin>>n2;
for(int i=0;i<n2;i++)
{
int temp;
cin>>temp;
l2.push_back(temp);
}
l1.merge(l2);
for(auto x = l1.begin();x!=l1.end();x++){
cout<<(*x)<<" ";
}
cout<<endl;
}
return 0;
}