-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_ll.cpp
More file actions
50 lines (42 loc) · 770 Bytes
/
Copy pathreverse_ll.cpp
File metadata and controls
50 lines (42 loc) · 770 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
#include <iostream>
#include <string>
struct node
{
int data;
node* next;
};
void print_ll(node* head)
{
while(head)
{
std::cout << head->data;
head = head->next;
}
std::cout << std::endl;
}
node* reverse_ll(node* head)
{
node* current, *trailing;
current = head;
trailing = nullptr;
while(current)
{
node* temp = current;
current = current->next;
temp->next = trailing;
trailing = temp;
}
return trailing;
}
int main()
{
node values[10];
for(int i = 0; i < 10; ++i)
{
values[i].data = i;
values[i].next = (i == 9 ? nullptr : &values[i+1]);
}
print_ll(values);
node* newhead = reverse_ll(values);
print_ll(newhead);
}