-
Notifications
You must be signed in to change notification settings - Fork 43
/
reverseLinkedlist.cpp
187 lines (163 loc) · 3.73 KB
/
reverseLinkedlist.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node*next;
Node(int data){
this->data=data;
next=NULL;
}
};
class Pair{
public:
Node* head;
Node* tail;
};
Pair reverseLL_2(Node* head){
if(head==NULL || head->next==NULL){
Pair an;
an.head=head;
an.tail=head;
return an;
}
Pair ans=reverseLL_2(head->next);
ans.tail->next=head;
head->next=NULL;
Pair an;
an.head=ans.head;
an.tail=head;
return an;
}
Node* reverseLL_Better(Node*head){
return reverseLL_2(head).head;
}
Node* enterElement(Node* head){
int data;
cin>>data;
Node* newNode=new Node(data);
if(head==NULL){
head=newNode;
return head;
}
Node* temp=head;
while (temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newNode;
return head;
}
Node* insertNode(Node* head){
cout<<"Enter the lenght of linked list you want to reverse"<<endl;
int lenght;
cin>>lenght;
cout<<"Enter data"<<endl;
for (int i = 0; i < lenght; i++)
{
head=enterElement(head);
}
return head;
}
void print(Node *head){
while (head!=NULL)
{
cout<<head->data<<" ";
head=head -> next;
}
cout<<endl;
}
Node* reverseList(Node* head){
if(head==NULL || head->next==NULL){
return head;
}
Node* ans=reverseList(head->next);
Node* temp=ans;
while (temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
head->next=NULL;
return ans;
}
Node* reverseList_3(Node* head){
if(head==NULL || head->next==NULL){
return head;
}
Node* ans=reverseList(head->next);
Node* tail=head->next;
tail->next=head;
head->next=NULL;
return ans;
}
Node* reverseLL_4(Node* head){
Node* cur=head;
Node* pre=NULL;
Node* next=cur;
if(head==NULL || head->next==NULL){
return head;
}
while (cur!=NULL)
{
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
head=pre;
return head;
}
int main()
{
Node* head=NULL;
head=insertNode(head);
cout<<"The entered linked list is "<<endl;
print(head);
back:
int input;
cout<<"Enter the method you want to use for reverse linkedlist"<<endl;
cout<<"Enter 1 for using recursion with time complixity of n^2"<<endl;
cout<<"Enter 2 for using recursion with time complixity of n and pair of return type using class"<<endl;
cout<<"Enter 3 for using recursion with time complixity of n using tail concept"<<endl;
cout<<"Enter 4 for using Iterative "<<endl;
cin>>input;
switch (input)
{
case 1:
{
head=reverseList(head);
cout<<"The reverse linked list is "<<endl;
print(head);
goto back;
break;
}
case 2:
{
head=reverseLL_Better(head);
cout<<"The reverse linked list is "<<endl;
print(head);
goto back;
break;
}
case 3:
{
head=reverseList_3(head);
cout<<"The reverse linked list is "<<endl;
print(head);
goto back;
break;
}
case 4:
{
head=reverseLL_4(head);
cout<<"The reverse linked list is "<<endl;
print(head);
goto back;
break;
}
default:
break;
}
return 0;
}