forked from msaimraz/Hacktoberfest_2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse_Linkedlist_Iterative.cpp
69 lines (57 loc) · 1.4 KB
/
Reverse_Linkedlist_Iterative.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
#include<bits/stdc++.h>
using namespace std;
//BELOW IS THE ITERATIVE APPROACH TO REVERSE GIVEB LINKED LIST
// node structure
class node{
public:
int data;
node *next;
node(int data){
this->data=data;
this->next=NULL;
}
};
//function to insert node at the end of linked list
void insertatend(int d,node* &tail){
node * newnode= new node(d);
tail->next=newnode;
tail=tail->next;
}
//function to print the linked list
void printlist(node* &head){
node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
//iterative approach to reverse given linked list- once the traversal and reversal is completed the head of the new reversed linked list will be stored into 'prev' variable so we return that
node* reverselist(node* &head){
//if list is empty or has only one element
if(head==NULL || head->next==NULL){
return head;
}
node* prev=NULL;
node* curr=head;
node* forward=NULL;
while(curr!=NULL){
forward=curr->next;
curr->next=prev;
prev=curr;
curr=forward;
}
return prev;
}
//main function
int main(){
node* node1=new node(3);
node * head=node1;
node * tail=node1;
insertatend(5,tail);
insertatend(7,tail);
insertatend(9,tail);
printlist(head);
reverselist(head);
printlist(head);
}