File tree Expand file tree Collapse file tree 5 files changed +117
-6
lines changed Expand file tree Collapse file tree 5 files changed +117
-6
lines changed Original file line number Diff line number Diff line change 46
46
- radix sort for d digit numbers
47
47
- find maximum sum subarray from an array
48
48
- find triplet that sums to given value using binary search technique
49
+ - make a circular linked list with a sentinel
50
+ - double linked list insertion all points and deletion
49
51
50
52
### General Questions
51
53
@@ -110,6 +112,9 @@ TODO:
110
112
- [ Program to create a singly linked list, insert and delete nodes and print all of them] ( /linked-lists/question1.c )
111
113
- [ Move the last node to the begining of a linked list] ( /linked-lists/question2.c )
112
114
- [ Traverse a Single linked list using recursion] ( /linked-lists/question3.c )
115
+ - [ Iterative program to reverse a linked list] ( /linked-lists/question4.c )
116
+ - [ Recursive program to reverse a linked list] ( /linked-lists/question5.c )
117
+
113
118
114
119
## Some important concepts to solve algos better
115
120
Original file line number Diff line number Diff line change @@ -44,16 +44,18 @@ int main(){
44
44
t = head ;
45
45
printList (t ,head );
46
46
struct node * p ;
47
+
47
48
while (t -> link ){
48
49
p = t ;
49
- t = t -> link ;
50
+ t = t -> link ;
50
51
}
51
- t -> link = NULL ;
52
- // free(t);
53
- p -> next = head ;
54
- head = p ;
55
52
56
- t = head ;
53
+ free (t );
54
+
55
+ t -> link = head ;
56
+ p -> link = NULL ;
57
+ head = t ;
58
+
57
59
printList (t ,head );
58
60
59
61
}
Original file line number Diff line number Diff line change
1
+ /*
2
+ Iterative program to reverse a linked list
3
+ */
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+
7
+ struct node {
8
+ int data ;
9
+ struct node * link ;
10
+ };
11
+
12
+ void printList (struct node * t ){
13
+ if (t ){
14
+ //interchanging these lines will print it in reverse order
15
+ printf ("%d\n" , t -> data );
16
+ printList (t -> link );
17
+ }
18
+ }
19
+
20
+ int main (){
21
+
22
+ struct node * head = (struct node * )malloc (sizeof (struct node ));
23
+
24
+ struct node * t = head ;
25
+
26
+ int counter = 1 ;
27
+ while (counter <=5 ){
28
+
29
+ t -> data = counter * 10 ;
30
+ if (counter == 5 ){
31
+ t -> link = NULL ;
32
+ }else {
33
+ t -> link = (struct node * )malloc (sizeof (struct node ));
34
+ }
35
+
36
+ t = t -> link ;
37
+
38
+ counter ++ ;
39
+ }
40
+ t = head ;
41
+ printList (t );
42
+ t = head ;
43
+
44
+ //reversing the list
45
+ struct node * curr , * prev ,* nextNode ;
46
+ curr = head ;
47
+
48
+ prev = NULL ;
49
+ while (curr ){
50
+ nextNode = curr -> link ;
51
+ curr -> link = prev ;
52
+ prev = curr ;
53
+ curr = nextNode ;
54
+
55
+ }
56
+ head = prev ;
57
+
58
+ printList (head );
59
+
60
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Recursive program to reverse a linked list
3
+ */
4
+
5
+ struct node {
6
+ int data ;
7
+ struct node * link ;
8
+ };
9
+
10
+ void printList (struct node * t ){
11
+ if (t ){
12
+ //interchanging these lines will print it in reverse order
13
+ printf ("%d\n" , t -> data );
14
+ printList (t -> link );
15
+ }
16
+ }
17
+
18
+ int main (){
19
+
20
+ struct node * head = (struct node * )malloc (sizeof (struct node ));
21
+
22
+ struct node * t = head ;
23
+
24
+ int counter = 1 ;
25
+ while (counter <=5 ){
26
+
27
+ t -> data = counter * 10 ;
28
+ if (counter == 5 ){
29
+ t -> link = NULL ;
30
+ }else {
31
+ t -> link = (struct node * )malloc (sizeof (struct node ));
32
+ }
33
+
34
+ t = t -> link ;
35
+
36
+ counter ++ ;
37
+ }
38
+ t = head ;
39
+ printList (t );
40
+ t = head ;
41
+
42
+
43
+
44
+ }
You can’t perform that action at this time.
0 commit comments