-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_ReverseLL.cpp
More file actions
67 lines (61 loc) · 1.28 KB
/
03_ReverseLL.cpp
File metadata and controls
67 lines (61 loc) · 1.28 KB
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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
class node {
public:
int data;
node *next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
void insertHead(node *&head, node *&tail, int data) {
if (head == NULL) {
node *temp = new node(data);
head = temp;
tail = temp;
return;
}
}
void insertTail(node *&head, node *&tail, int data) {
if (tail == NULL) {
node *temp = new node(data);
head = temp;
tail = temp;
return;
}
node *temp = new node(data);
tail->next = temp;
tail = temp;
}
void printLL(node *&head) {
node *temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void reverseLL(node *&head) {
node *prev = NULL, *curr = head, *forward = curr;
while (curr != NULL) {
forward = curr->next;
curr->next = prev;
prev = curr;
curr = forward;
}
head = prev;
}
int main() {
node *head = NULL, *tail = NULL;
insertTail(head, tail, 1);
insertTail(head, tail, 3);
insertTail(head, tail, 5);
insertTail(head, tail, 7);
printLL(head);
reverseLL(head);
printLL(head);
return 0;
}