forked from Ayu-99/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse a LL(Iteratively).cpp
More file actions
45 lines (38 loc) · 871 Bytes
/
Reverse a LL(Iteratively).cpp
File metadata and controls
45 lines (38 loc) · 871 Bytes
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
/*
Code: Reverse LL (Iterative)
Send Feedback
Given a linked list, reverse it iteratively.
You don't need to print the elements, just reverse the LL duplicates and return the head of updated LL.
Input format : Linked list elements (separated by space and terminated by -1)`
Sample Input 1 :
1 2 3 4 5 -1
Sample Output 1 :
5 4 3 2 1
*/
// Following is the node structure
/**************
class node{
public:
int data;
node * next;
node(int data){
this->data=data;
this->next=NULL;
}
};
***************/
node* rev_linkedlist_itr(node* head)
{
//write your iterative code here
node *current=head;
node *next=NULL;
node *prev=NULL;
while(current!=NULL){
next=current->next;
current->next=prev;
prev=current;
current=next;
}
// head=prev;
return prev;
}