-
Notifications
You must be signed in to change notification settings - Fork 2
/
reverse_singly_linklist.cpp
60 lines (55 loc) · 1020 Bytes
/
reverse_singly_linklist.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
#include "iostream"
using namespace std;
struct Node
{
struct Node *next;
int data;
};
Node *head = NULL;
void insert(Node * temp, int data)
{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new Node;
temp = temp->next;
temp->data = data;
temp->next = NULL;
}
void display(Node *temp)
{
while(temp!=NULL)
{
cout<<"data : "<<temp->data<<endl;
temp= temp->next;
}
cout<<"--------------------------------------\n";
}
Node *reverseTheList(Node* temp)
{
Node *prev=NULL, *curr= temp, *next = NULL;
while(curr->next != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
curr->next = prev;
head = curr;
}
int main()
{
head = new Node;
head->data = 1;
head->next = NULL;
insert(head, 2);
insert(head, 3);
insert(head, 4);
insert(head, 5);
insert(head, 6);
display(head);
reverseTheList(head);
display(head);
}