Skip to content

Commit 8c48b9a

Browse files
committed
Added new solutions
1 parent 411245c commit 8c48b9a

File tree

20 files changed

+1294
-0
lines changed

20 files changed

+1294
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
struct Node* createNode(int data) {
10+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11+
newNode->data = data;
12+
newNode->next = NULL;
13+
return newNode;
14+
}
15+
16+
void printList(struct Node* head) {
17+
while (head) {
18+
printf("%d -> ", head->data);
19+
head = head->next;
20+
}
21+
printf("NULL\n");
22+
}
23+
24+
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
25+
if (!l1) return l2;
26+
if (!l2) return l1;
27+
28+
if (l1->data < l2->data) {
29+
l1->next = mergeSortedLists(l1->next, l2);
30+
return l1;
31+
} else {
32+
l2->next = mergeSortedLists(l1, l2->next);
33+
return l2;
34+
}
35+
}
36+
37+
int main() {
38+
int n1, n2, value;
39+
40+
printf("Enter the number of nodes in the first sorted list: ");
41+
scanf("%d", &n1);
42+
struct Node *head1 = NULL, *tail1 = NULL;
43+
44+
for (int i = 0; i < n1; i++) {
45+
printf("Enter value for node %d: ", i + 1);
46+
scanf("%d", &value);
47+
struct Node* newNode = createNode(value);
48+
if (!head1) {
49+
head1 = tail1 = newNode;
50+
} else {
51+
tail1->next = newNode;
52+
tail1 = newNode;
53+
}
54+
}
55+
56+
printf("Enter the number of nodes in the second sorted list: ");
57+
scanf("%d", &n2);
58+
struct Node *head2 = NULL, *tail2 = NULL;
59+
60+
for (int i = 0; i < n2; i++) {
61+
printf("Enter value for node %d: ", i + 1);
62+
scanf("%d", &value);
63+
struct Node* newNode = createNode(value);
64+
if (!head2) {
65+
head2 = tail2 = newNode;
66+
} else {
67+
tail2->next = newNode;
68+
tail2 = newNode;
69+
}
70+
}
71+
72+
printf("First List: ");
73+
printList(head1);
74+
printf("Second List: ");
75+
printList(head2);
76+
77+
struct Node* mergedList = mergeSortedLists(head1, head2);
78+
printf("Merged List: ");
79+
printList(mergedList);
80+
81+
return 0;
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
struct Node* createNode(int data) {
10+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11+
newNode->data = data;
12+
newNode->next = NULL;
13+
return newNode;
14+
}
15+
16+
void printList(struct Node* head) {
17+
while (head) {
18+
printf("%d -> ", head->data);
19+
head = head->next;
20+
}
21+
printf("NULL\n");
22+
}
23+
24+
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
25+
if (!l1) return l2;
26+
if (!l2) return l1;
27+
28+
if (l1->data < l2->data) {
29+
l1->next = mergeSortedLists(l1->next, l2);
30+
return l1;
31+
} else {
32+
l2->next = mergeSortedLists(l1, l2->next);
33+
return l2;
34+
}
35+
}
36+
37+
int main() {
38+
int n1, n2, value;
39+
40+
printf("Enter the number of nodes in the first sorted list: ");
41+
scanf("%d", &n1);
42+
struct Node *head1 = NULL, *tail1 = NULL;
43+
44+
for (int i = 0; i < n1; i++) {
45+
printf("Enter value for node %d: ", i + 1);
46+
scanf("%d", &value);
47+
struct Node* newNode = createNode(value);
48+
if (!head1) {
49+
head1 = tail1 = newNode;
50+
} else {
51+
tail1->next = newNode;
52+
tail1 = newNode;
53+
}
54+
}
55+
56+
printf("Enter the number of nodes in the second sorted list: ");
57+
scanf("%d", &n2);
58+
struct Node *head2 = NULL, *tail2 = NULL;
59+
60+
for (int i = 0; i < n2; i++) {
61+
printf("Enter value for node %d: ", i + 1);
62+
scanf("%d", &value);
63+
struct Node* newNode = createNode(value);
64+
if (!head2) {
65+
head2 = tail2 = newNode;
66+
} else {
67+
tail2->next = newNode;
68+
tail2 = newNode;
69+
}
70+
}
71+
72+
printf("First List: ");
73+
printList(head1);
74+
printf("Second List: ");
75+
printList(head2);
76+
77+
struct Node* mergedList = mergeSortedLists(head1, head2);
78+
printf("Merged List: ");
79+
printList(mergedList);
80+
81+
return 0;
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
struct Node* createNode(int data) {
10+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11+
newNode->data = data;
12+
newNode->next = NULL;
13+
return newNode;
14+
}
15+
16+
void printList(struct Node* head) {
17+
while (head) {
18+
printf("%d -> ", head->data);
19+
head = head->next;
20+
}
21+
printf("NULL\n");
22+
}
23+
24+
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
25+
if (!l1) return l2;
26+
if (!l2) return l1;
27+
28+
if (l1->data < l2->data) {
29+
l1->next = mergeSortedLists(l1->next, l2);
30+
return l1;
31+
} else {
32+
l2->next = mergeSortedLists(l1, l2->next);
33+
return l2;
34+
}
35+
}
36+
37+
int main() {
38+
int n1, n2, value;
39+
40+
printf("Enter the number of nodes in the first sorted list: ");
41+
scanf("%d", &n1);
42+
struct Node *head1 = NULL, *tail1 = NULL;
43+
44+
for (int i = 0; i < n1; i++) {
45+
printf("Enter value for node %d: ", i + 1);
46+
scanf("%d", &value);
47+
struct Node* newNode = createNode(value);
48+
if (!head1) {
49+
head1 = tail1 = newNode;
50+
} else {
51+
tail1->next = newNode;
52+
tail1 = newNode;
53+
}
54+
}
55+
56+
printf("Enter the number of nodes in the second sorted list: ");
57+
scanf("%d", &n2);
58+
struct Node *head2 = NULL, *tail2 = NULL;
59+
60+
for (int i = 0; i < n2; i++) {
61+
printf("Enter value for node %d: ", i + 1);
62+
scanf("%d", &value);
63+
struct Node* newNode = createNode(value);
64+
if (!head2) {
65+
head2 = tail2 = newNode;
66+
} else {
67+
tail2->next = newNode;
68+
tail2 = newNode;
69+
}
70+
}
71+
72+
printf("First List: ");
73+
printList(head1);
74+
printf("Second List: ");
75+
printList(head2);
76+
77+
struct Node* mergedList = mergeSortedLists(head1, head2);
78+
printf("Merged List: ");
79+
printList(mergedList);
80+
81+
return 0;
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
def print_list(head):
7+
current = head
8+
while current:
9+
print(current.data, end=" -> ")
10+
current = current.next
11+
print("NULL")
12+
13+
def merge_sorted_lists(l1, l2):
14+
if not l1:
15+
return l2
16+
if not l2:
17+
return l1
18+
19+
if l1.data < l2.data:
20+
l1.next = merge_sorted_lists(l1.next, l2)
21+
return l1
22+
else:
23+
l2.next = merge_sorted_lists(l1, l2.next)
24+
return l2
25+
26+
if __name__ == "__main__":
27+
n1 = int(input("Enter the number of nodes in the first sorted list: "))
28+
head1 = None
29+
tail1 = None
30+
31+
for i in range(n1):
32+
value = int(input(f"Enter value for node {i + 1}: "))
33+
new_node = Node(value)
34+
if not head1:
35+
head1 = tail1 = new_node
36+
else:
37+
tail1.next = new_node
38+
tail1 = new_node
39+
40+
n2 = int(input("Enter the number of nodes in the second sorted list: "))
41+
head2 = None
42+
tail2 = None
43+
44+
for i in range(n2):
45+
value = int(input(f"Enter value for node {i + 1}: "))
46+
new_node = Node(value)
47+
if not head2:
48+
head2 = tail2 = new_node
49+
else:
50+
tail2.next = new_node
51+
tail2 = new_node
52+
53+
print("First List:")
54+
print_list(head1)
55+
print("Second List:")
56+
print_list(head2)
57+
58+
merged_list = merge_sorted_lists(head1, head2)
59+
print("Merged List:")
60+
print_list(merged_list)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
struct Node* createNode(int data) {
10+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11+
newNode->data = data;
12+
newNode->next = NULL;
13+
return newNode;
14+
}
15+
16+
void printList(struct Node* head) {
17+
while (head) {
18+
printf("%d -> ", head->data);
19+
head = head->next;
20+
}
21+
printf("NULL\n");
22+
}
23+
24+
struct Node* removeNthFromEnd(struct Node* head, int n) {
25+
struct Node* dummy = createNode(0);
26+
dummy->next = head;
27+
struct Node *fast = dummy, *slow = dummy;
28+
29+
for (int i = 0; i <= n; i++) {
30+
if (fast) fast = fast->next;
31+
}
32+
33+
while (fast) {
34+
fast = fast->next;
35+
slow = slow->next;
36+
}
37+
38+
struct Node* toDelete = slow->next;
39+
slow->next = toDelete->next;
40+
free(toDelete);
41+
42+
struct Node* newHead = dummy->next;
43+
free(dummy);
44+
return newHead;
45+
}
46+
47+
int main() {
48+
int n, value, pos;
49+
50+
printf("Enter the number of nodes: ");
51+
scanf("%d", &n);
52+
53+
struct Node *head = NULL, *tail = NULL;
54+
for (int i = 0; i < n; i++) {
55+
printf("Enter value for node %d: ", i + 1);
56+
scanf("%d", &value);
57+
struct Node* newNode = createNode(value);
58+
if (!head) {
59+
head = tail = newNode;
60+
} else {
61+
tail->next = newNode;
62+
tail = newNode;
63+
}
64+
}
65+
66+
printf("Enter the position from the end to remove: ");
67+
scanf("%d", &pos);
68+
69+
printf("Original List: ");
70+
printList(head);
71+
72+
head = removeNthFromEnd(head, pos);
73+
74+
printf("Updated List: ");
75+
printList(head);
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)