-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseLinkedList.cpp
More file actions
58 lines (42 loc) · 820 Bytes
/
reverseLinkedList.cpp
File metadata and controls
58 lines (42 loc) · 820 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
// number = min + ( rand() % (int)( max - min + 1));
class node {
public:
int v;
node * next;
node(int _v): v(_v) {}
};
node * getLinkedList(int size) {
node *head = nullptr;
for (int i = 0; i < size; i++ )
{
node *tmp;
tmp = new node(i);
tmp -> next = head;
head = tmp;
}
return head;
}
node* reverse(node * head) {
node* newHead = nullptr;
while (head) {
node* next = head -> next;
head -> next = newHead;
newHead = head;
head = next;
}
return newHead;
}
void printList(const node *head) {
while ( head -> next) {
cout << head -> v << endl;
head = head -> next;
}
}
int main() {
printList(getLinkedList(10));
cout << "\n After Reverse Applied\n";
printList(reverse(getLinkedList(10)));
return 0;
}