Skip to content

Commit bce9e2a

Browse files
committed
new question added
1 parent 629d2d4 commit bce9e2a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

linked-lists/question5.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
Recursive program to reverse a linked list
33
*/
44

5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
58
struct node{
69
int data;
710
struct node *link;
811
};
912

13+
struct node *head;
14+
1015
void printList(struct node *t){
1116
if(t){
1217
//interchanging these lines will print it in reverse order
@@ -15,9 +20,18 @@ void printList(struct node *t){
1520
}
1621
}
1722

23+
void reverse(struct node *prev, struct node *curr){
24+
if(curr){
25+
reverse(curr, curr->link);
26+
curr->link = prev;
27+
}else{
28+
head = prev;
29+
}
30+
}
31+
1832
int main(){
1933

20-
struct node *head = (struct node *)malloc(sizeof(struct node));
34+
head = (struct node *)malloc(sizeof(struct node));
2135

2236
struct node *t = head;
2337

@@ -39,6 +53,7 @@ int main(){
3953
printList(t);
4054
t = head;
4155

42-
43-
56+
reverse(NULL,head);
57+
t = head;
58+
printList(t);
4459
}

0 commit comments

Comments
 (0)