-
Notifications
You must be signed in to change notification settings - Fork 23
/
reverse_Linked_list.cpp
53 lines (47 loc) · 1.09 KB
/
reverse_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
#include<bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node *next;
};
void push(struct node **head_ref, int data) {
struct node *node;
node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->next = (*head_ref);
(*head_ref) = node;
}
void reverse(struct node **head_ref) {
struct node *temp = NULL;
struct node *prev = NULL;
struct node *current = (*head_ref);
while(current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
(*head_ref) = prev;
}
void printnodes(struct node *head) {
while(head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
struct node *head = NULL;
push(&head, 0);
push(&head, 1);
push(&head, 8);
push(&head, 0);
push(&head, 4);
push(&head, 10);
cout << "Linked List Before Reversing" << endl;
printnodes(head);
reverse(&head);
cout << endl;
cout << "Linked List After Reversing"<<endl;
printnodes(head);
return 0;
}