-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterativeReverseSingleLL.cpp
77 lines (60 loc) · 1.45 KB
/
IterativeReverseSingleLL.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Iteratively reverse a singly linked list.
//
// O(1) storage and O(n) time complexity.
//
// Complier: Visual Studio 2013 (v120)
#include <iostream>
#include <memory>
using namespace std;
template<typename T>
struct Node
{
shared_ptr<Node<T>> next;
T val;
};
template <typename T>
shared_ptr<Node<T>> build_list(int size) {
auto head = shared_ptr<Node<T>>(new Node<T>());
head->val = 0;
shared_ptr<Node<T>> current = head;
for (int i = 1; i <= size; i++) {
auto next = shared_ptr<Node<T>>(new Node<T>());
next->val = i;
current->next = next;
current = next;
}
current->next == nullptr;
return head;
}
template <typename T>
void display_list(shared_ptr<Node<T>> head) {
auto current = head;
cout << endl << "[HEAD]";
while (current) {
std::cout << "->" << current->val;
current = current->next;
}
}
template <typename T>
shared_ptr<Node<T>> reverse_list(const shared_ptr<Node<T>>& head) {
auto current = head;
shared_ptr<Node<T>> prev = nullptr;
while (current) {
// change current->next so it points a previous element
shared_ptr<Node<T>> before = current->next;
current->next = prev;
// Move ahead a step
prev = current;
current = before;
}
return prev;
}
void main()
{
auto head = build_list<int>(10);
display_list<int>(head);
auto reverse = reverse_list<int>(head);
display_list<int>(reverse);
std::cout << std::endl << "[Press enter to exit]";
std::cin.ignore();
}