Skip to content

Commit 629d2d4

Browse files
committed
new questions added
1 parent ef9b3f9 commit 629d2d4

File tree

5 files changed

+117
-6
lines changed

5 files changed

+117
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ TODO:
4646
- radix sort for d digit numbers
4747
- find maximum sum subarray from an array
4848
- 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
4951

5052
### General Questions
5153

@@ -110,6 +112,9 @@ TODO:
110112
- [Program to create a singly linked list, insert and delete nodes and print all of them](/linked-lists/question1.c)
111113
- [Move the last node to the begining of a linked list](/linked-lists/question2.c)
112114
- [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+
113118

114119
## Some important concepts to solve algos better
115120

linked-lists/question2.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ int main(){
4444
t = head;
4545
printList(t,head);
4646
struct node *p;
47+
4748
while(t->link){
4849
p = t;
49-
t = t->link;
50+
t=t->link;
5051
}
51-
t->link = NULL;
52-
// free(t);
53-
p->next = head;
54-
head = p;
5552

56-
t = head;
53+
free(t);
54+
55+
t->link = head;
56+
p->link = NULL;
57+
head = t;
58+
5759
printList(t,head);
5860

5961
}

linked-lists/question4.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

linked-lists/question5.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

linked-lists/question6.c

Whitespace-only changes.

0 commit comments

Comments
 (0)