-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevrselinkL.c
79 lines (57 loc) · 1.78 KB
/
revrselinkL.c
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
78
79
#include<stdio.h>
#include<stdlib.h>
// construct a structure and typedef to node
typedef struct node{
int data;
struct node *next;
}node;
// Create a node
node* createNode(int new_data) {
node *new_node = (node*)malloc(sizeof(node));// Mmory allocate
if(!new_node){
printf("Memory is not allocated");
exit(0);
}
new_node->data = new_data;// put data
new_node->next = NULL;
return new_node;
}
// Print the list
void print(node *n){
while( n!= NULL){
printf("%d\n",n->data);
n=n->next;
}
}
// reverse the list
node* reverselist(node* head){
node* arr[10000]; // create a stack
int top=-1;
node* x = head; // x is a temporary node or dupliacte node
while(x!=NULL){
arr[++top]=x; // array me x ki value daalna hai
x = x->next; // x me aage wale vala node ka address
}
if(top>=0){
head = arr[top]; // ab jo array me top me hoga use head me daal denge
x = head; // head ko phirse x me daal denge
while (top>0)
{
top--; //top = top-1
x->next = arr[top]; // ab jo upar se niche vala hai uska address vala arraay me
x = x->next; // x me x ka next
}
x->next = NULL; // last x ke null
}
return head; // head ko return
}
int main(){
node *head=createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next=createNode(4);
head->next->next->next->next=createNode(5);
print(head);
head=reverselist(head);
print(head);
}