File tree 2 files changed +48
-1
lines changed
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ TODO:
109
109
110
110
- [ Program to create a singly linked list, insert and delete nodes and print all of them] ( /linked-lists/question1.c )
111
111
- [ 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 )
113
113
114
114
## Some important concepts to solve algos better
115
115
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments