-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertTheLinkList.cpp
146 lines (129 loc) · 3.39 KB
/
convertTheLinkList.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
/*
Given Singly linked list (1->2->3->4->5->6) , convert this list to (1->6->2->5->3->4).
1st's element next is nth element, 2nd's next is n-1th element and so on.
Ans Approach
1) Split linked list into 2 halfs
2) Reverse the 2nd half.
3) Now merge 1st and 2nd half again as per the question.
*/
/*
OUTPUT ->
Input list
1 2 3 4 5 6
--------------------------------------
First half list :
1 2 3
--------------------------------------
Second half list :
6 5 4
--------------------------------------
Answer :
1 6 2 5 3 4
--------------------------------------
*/
#include "iostream"
using namespace std;
struct Node
{
struct Node *next;
int data;
};
Node *head = NULL;
void insert(Node * temp, int data)
{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new Node;
temp = temp->next;
temp->data = data;
temp->next = NULL;
}
void display(Node *temp)
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp= temp->next;
}
cout<<"\n--------------------------------------\n";
}
Node *reverseTheList(Node* temp)
{
Node *prev=NULL, *curr= temp, *next = NULL;
while(curr->next != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
curr->next = prev;
return curr;
}
Node* convertTheList(Node * list1, Node *list2)
{
// fitst we need to add first two element here & then go inside the loop
// reason : we need the head of newly created list;
Node *head3 = new Node;
Node *newHead = head3;//store the head address of list
head3->data = list1->data;
head3->next = new Node; head3 = head3->next;head3->data = list2->data;
list1 = list1->next;
list2 = list2->next;
//NOTE: as per que, both the input list should have same size
// if not then we need to add extra checks to add remaining list.
while(list1 != NULL && list2 != NULL)
{
head3->next = new Node;
head3 = head3->next; head3->data= list1->data; // storing list1 data
head3->next = new Node;
head3 = head3->next; head3->data= list2->data; // storing list2 data
list1 = list1->next;
list2 = list2->next;
}
head3->next = NULL;// to terminate the newly created list;
return newHead;
}
int main()
{
head = new Node;
head->data = 1;
head->next = NULL;
insert(head, 2);
insert(head, 3);
insert(head, 4);
insert(head, 5);
insert(head, 6);//1, 6, 2, 5, 3, 4
cout<<"Input list \n";
display(head);
Node * head1;
int i =0;
Node *temp = head;
//consider length is 3 / we can also calculate by writing length function
int length = 6;
//break the linkList
while(i != length/2)
{
if(i == ((length/2) -1))
{
head1=temp->next;
temp->next=NULL;
break;
}
temp = temp->next;
i++;
}
//head1 : fist half list
//head2 : second half list
cout<<"First half list : \n";
display(head);
cout<<"Second half list : \n";
display(head1);
head1=reverseTheList(head1);// reverse the second half list
cout<<"Second half reversed list : \n";
display(head1); //display second reversed list
cout<<"Answer : \n";
display(convertTheList(head, head1)); // convert the list as per the question
}