-
Notifications
You must be signed in to change notification settings - Fork 618
/
reverse_a_linked_list.cpp
122 lines (112 loc) · 2.59 KB
/
reverse_a_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
/*
The following algorithm is used for reversing a linked list:
1. Initialize three pointers: prev, curr and ahead.
2. curr points to the current node we're at, prev to its previous node and ahead points to the node that comes after curr.
4. Initially curr = head and prev = nullptr.
3. We traverse the linked list until curr becomes null.
4. Make ahead = curr->next
5. Now make curr's pointer point to prev i.e. curr->next = prev
6. Move prev one step further i.e. prev = curr
7. Move curr one step further i.e curr = ahead
8. After the loop terminates, prev will be pointing to the new head i.e the last node in the list. So make head = prev
*/
#include<bits/stdc++.h>
using namespace std;
//Defining the structure of a node
class Node{
public:
int data;
Node *next;
};
class linked_list{
//Head points to the first node in the list
Node *head;
public:
linked_list(){
head = nullptr;
}
//This method inserts a node in the beginning of the list
void insert_at_beginning(int data){
if(head==nullptr){
head = new Node;
head->data = data;
head->next = nullptr;
return;
}
Node *new_node = new Node;
new_node->data = data;
new_node->next = head;
head = new_node;
}
//This method prints the list
void print_list(){
Node *n = head;
while(n!=nullptr){
cout<<n->data<<" ";
n=n->next;
}cout<<endl;
}
//Method for reversing the list
void reverse_list(){
Node *prev = nullptr, *curr = head, *ahead;
while(curr!=nullptr){
ahead = curr->next;
curr->next = prev;
prev=curr;
curr=ahead;
}
head = prev;
}
};
int main(){
//Number of nodes
int n; cin>>n;
linked_list a;
int value;
//Constructing the linked list
for(int i=0;i<n;i++){
cin>>value;
a.insert_at_beginning(value);
}
cout<<"Your list: "<<endl;
//Prints the inputed list
a.print_list();
//Reverses the list
a.reverse_list();
cout<<"Reversed list "<<endl;
//Prints the reversed list
a.print_list();
return 0;
}
/*
Test Cases:
1.
Input:
5
10 20 30 40 50
Output:
Your list:
50 40 30 20 10
Reversed list
10 20 30 40 50
2.
Input:
1
10
Output:
Your list:
10
Reversed list
10
3.
Input:
8
10 20 30 40 50 60 70 80
Output:
Your list:
80 70 60 50 40 30 20 10
Reversed list
10 20 30 40 50 60 70 80
Time Complexity : O(n) n is the number of nodes.
Space Complexity : O(1)
*/