-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_link_list.cpp
40 lines (33 loc) · 1020 Bytes
/
reverse_link_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
#include "data_struct_types.h"
#include "print_utils.h"
using namespace print_utils;
using namespace data_struct_types;
template <typename T>
LinkListNode<T>* ReverseLinkList(LinkListNode<T> *root)
{
LinkListNode<T> *head = nullptr;
LinkListNode<T> *next = nullptr;
LinkListNode<T> *curr = root;
while (curr != nullptr)
{
next = curr->next;
curr->next = head;
head = curr;
curr = next;
}
return head;
}
int main(int argc, char const *argv[])
{
LinkListNode<int> *root = new LinkListNode<int>(0);
root->next = new LinkListNode<int>(1);
root->next->next = new LinkListNode<int>(2);
root->next->next->next = new LinkListNode<int>(3);
root->next->next->next->next = new LinkListNode<int>(4);
std::cout << "given link list:" << std::endl;
std::cout << root << std::endl;
auto new_root = ReverseLinkList(root);
std::cout << "output reversed link list:" << std::endl;
std::cout << new_root << std::endl;
return 0;
}