forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alternate_merging_of_linked_list.cpp
157 lines (136 loc) · 3.2 KB
/
alternate_merging_of_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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Author: Mohim Singla
C++ program to merge alternate nodes two linked lists.
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
//Creating a struct
struct node{
int data;
node *next;
};
//Function to create linked list
struct node* create(int n)
{
struct node* head=NULL;
struct node* tail=NULL;
while(n!=0)
{
int x;
cin>>x;
if (head==NULL)
{
node* temp=new node;
temp->data=x;
temp->next=NULL;
head=temp;
tail=head;
}
else
{
node* temp=new node;
temp->data=x;
temp->next=NULL;
tail->next=temp;
tail=temp;
}
n--;
}
return head;
}
struct node* head=NULL;
struct node* tail=NULL;
//Function to create linked list
struct node* new_list(int x)
{
if (head==NULL)
{
node* temp=new node;
temp->data=x;
temp->next=NULL;
head=temp;
tail=head;
}
else
{
node* temp=new node;
temp->data=x;
temp->next=NULL;
tail->next=temp;
tail=temp;
}
return head;
};
//Create Merged linked list function
struct node* merge_list(struct node* list1, struct node* list2,int y)
{
node* list3=NULL;
node* temp1=list1;
node* temp2=list2;
while(y!=0)
{
list3=new_list(temp1->data);
list3=new_list(temp2->data);
temp1=temp1->next;
temp2=temp2->next;
y--;
}
return list3;
}
//display function to print the linked lists
void display(struct node* head)
{
node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
//main starts
int main()
{
cout<<"Enter no. of nodes in both the linked lists: ";
int y;
cin>>y;
node* list1=create(y);
node* list2=create(y);
cout<<"Linked List1: ";
display(list1);
cout<<"Linked List2: ";
display(list2);
node* list3=merge_list(list1,list2,y);
cout<<"Linked List after merging: ";
display(list3);
}
/*
Input format in test cases:
1st line contains number of nodes in each linked list(n).
2nd line contains value of each node of 1st linked list (total n) separated by spaces.
3rd line contains value of each node of 2nd linked list (total n) separated by spaces.
--------------------------------------------------------------------------------------
Test Case 1:
Input:
6
1 3 5 7 9 11
2 4 6 8 10 12
Output:
Linked List1: 1->3->5->7->9->11->NULL
Linked List2: 2->4->6->8->10->12->NULL
Linked List after merging: 1->2->3->4->5->6->7->8->9->10->11->12->NULL
--------------------------------------------------------------------------------------
Test Case 2:
Input:
9
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
Output:
Linked List1: 1->2->3->4->5->6->7->8->9->NULL
Linked List2: 9->8->7->6->5->4->3->2->1->NULL
Linked List after merging: 1->9->2->8->3->7->4->6->5->5->6->4->7->3->8->2->9->1->NULL
--------------------------------------------------------------------------------------
Time Complexity: merge_list() function: O(n)
Space complexity: merge_list() function: O(n)
*/