-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLL-Reverse_Iterative.c
More file actions
56 lines (54 loc) · 927 Bytes
/
LL-Reverse_Iterative.c
File metadata and controls
56 lines (54 loc) · 927 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
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void insert (int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next!=NULL) {
temp2 = temp2->next;
}
temp2->next = temp;
}
void Reverse () {
struct Node* current,*next,*prev;
current = head;
prev = NULL;
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void print () {
struct Node* temp = head;
while(temp!=NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main(void) {
head = NULL;
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
print();
Reverse();
print();
return 0;
}