-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
reversal_ll.c
53 lines (52 loc) · 953 Bytes
/
reversal_ll.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Node{
int value;
struct Node*next;
};
struct Node*head;
void reverse(struct Node**head)
{
struct Node*pre=NULL;
struct Node*next=NULL;
struct Node*current=*head;
while(current!=NULL)
{
next=current->next;
current->next=pre;
pre=current;
current=next;
}
*head=pre;
}
void insertatbeg(struct Node**head,int key)
{
struct Node*newnode=malloc(sizeof(struct Node));
newnode->value=key;
newnode->next=*head;
*head=newnode;
}
void print(void)
{
struct Node*temp=head;
while(temp!=NULL)
{
printf("%d",temp->value);
printf("-->");
temp=temp->next;
}
printf("NULL");
}
int main(void)
{
insertatbeg(&head,1);
insertatbeg(&head,2);
insertatbeg(&head,3);
insertatbeg(&head,4);
insertatbeg(&head,5);
print();
reverse(&head);
print();
return 0;
}