File tree 1 file changed +72
-0
lines changed
1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Node {
5
+ public:
6
+ int data;
7
+ Node* next;
8
+ Node* prev;
9
+ };
10
+
11
+ void reverse (Node** head_ref)
12
+ {
13
+ Node* temp = NULL ;
14
+ Node* current = *head_ref;
15
+
16
+ while (current != NULL ) {
17
+ temp = current->prev ;
18
+ current->prev = current->next ;
19
+ current->next = temp;
20
+ current = current->prev ;
21
+ }
22
+
23
+
24
+ if (temp != NULL )
25
+ *head_ref = temp->prev ;
26
+ }
27
+
28
+
29
+ void push (Node** head_ref, int new_data)
30
+ {
31
+ Node* new_node = new Node ();
32
+
33
+ new_node->data = new_data;
34
+
35
+ new_node->prev = NULL ;
36
+
37
+ new_node->next = (*head_ref);
38
+
39
+ if ((*head_ref) != NULL )
40
+ (*head_ref)->prev = new_node;
41
+
42
+ (*head_ref) = new_node;
43
+ }
44
+
45
+
46
+ void printList (Node* node)
47
+ {
48
+ while (node != NULL ) {
49
+ cout << node->data << " " ;
50
+ node = node->next ;
51
+ }
52
+ }
53
+
54
+ int main ()
55
+ {
56
+ Node* head = NULL ;
57
+
58
+ push (&head, 2 );
59
+ push (&head, 4 );
60
+ push (&head, 8 );
61
+ push (&head, 10 );
62
+
63
+ cout << " Original Linked list" << endl;
64
+ printList (head);
65
+
66
+ reverse (&head);
67
+
68
+ cout << " \n Reversed Linked list" << endl;
69
+ printList (head);
70
+
71
+ return 0 ;
72
+ }
You can’t perform that action at this time.
0 commit comments