Skip to content

Commit ef9b3f9

Browse files
committed
new question added
1 parent 3965c84 commit ef9b3f9

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ TODO:
109109

110110
- [Program to create a singly linked list, insert and delete nodes and print all of them](/linked-lists/question1.c)
111111
- [Move the last node to the begining of a linked list](/linked-lists/question2.c)
112-
- [Delete a node from linked-list](/linked-lists/question3.c)
112+
- [Traverse a Single linked list using recursion](/linked-lists/question3.c)
113113

114114
## Some important concepts to solve algos better
115115

linked-lists/question3.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Traverse a Single linked list using recursion
3+
Time complexity: O(n)
4+
5+
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
struct node{
12+
int data;
13+
struct node *link;
14+
};
15+
16+
void printList(struct node *t){
17+
if(t){
18+
//interchanging these lines will print it in reverse order
19+
printf("%d\n", t->data);
20+
printList(t->link);
21+
}
22+
}
23+
24+
int main(){
25+
26+
struct node *head = (struct node *)malloc(sizeof(struct node));
27+
28+
struct node *t = head;
29+
30+
int counter = 1;
31+
while(counter<=5){
32+
33+
t->data = counter*10;
34+
if(counter == 5){
35+
t->link = NULL;
36+
}else{
37+
t->link = (struct node *)malloc(sizeof(struct node));
38+
}
39+
40+
t = t->link;
41+
42+
counter++;
43+
}
44+
t = head;
45+
printList(t);
46+
47+
}

0 commit comments

Comments
 (0)