-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-a-linkedlist-using-loop.cpp
88 lines (77 loc) · 1.98 KB
/
reverse-a-linkedlist-using-loop.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
//We will not use swapping for this, as it is expensive so using it
//is a bad idea.
#include<iostream>
using namespace std;
//with the help of operator overloading, there is no need to write the
//print function for every linkedlist.
class node{
public:
int data;
node* next;
node(int data){
this -> data = data;
this -> next = NULL;
}
};
void insertAtHead(node* &head, int data){
if(head==NULL){
head = new node(data);
return;
}
//1. make a new node for the given data
node* n = new node(data);
//2. store the address of head in the second box of node
n->next = head; //(*n).next = head
//3. make the newly created node as the head of the list
head = n;
}
void print(node* head){
while(head!=NULL){
cout<<head -> data<<" -> ";
head = head->next;
}
cout<<"NULL";
cout<<endl;
}
node* inputdata(){
int data;
cin>>data;
node* head=NULL;
while(data!=-1){
insertAtHead(head,data);
cin>>data;
}
return head;
}
ostream& operator<<(ostream &p, node* head){ //since we didn't had to modify head, so we passed it by value
print(head);
return p;// it will return cout
}
istream& operator>>(istream &k,node*&head){ //since we have to modify the head, so we will pass it by reference.
head=inputdata();
return k;
}
//O(N) time and O(1) stack (more efficient than recursive)
void reverselinkedlist(node*&head){
node* prev = NULL;
node* current = head;
node* n; //next node
while(current!=NULL){
//save the next node
n = current->next;
//make the current node point to prev
current->next=prev;
prev=current;
current=n;
}
head=prev;
}
int main(){
node* head;
cin>>head;
cout<<head;
reverselinkedlist(head);
cout<<head;
//=>cout<<head2 (once head has been printed)
return 0;
}