-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge K sorted linked lists #22
Labels
Comments
Please assign me with this issue |
Assigned, please create a PR within 2 days |
Please create PR request not a reply
…On Sat, 28 Oct, 2023, 4:20 PM Payalchandak5, ***@***.***> wrote:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* mergeTwoLists(struct Node* list1, struct Node* list2) {
struct Node dummy;
struct Node* tail = &dummy;
dummy.next = NULL;
while (1) {
if (list1 == NULL) {
tail->next = list2;
break;
} else if (list2 == NULL) {
tail->next = list1;
break;
}
if (list1->data <= list2->data) {
tail->next = list1;
list1 = list1->next;
} else {
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
return dummy.next;
}
struct Node* mergeKSortedLists(struct Node* lists[], int k) {
if (k == 0) return NULL;
int last = k - 1;
while (last != 0) {
int i = 0, j = last;
while (i < j) {
lists[i] = mergeTwoLists(lists[i], lists[j]);
i++, j--;
if (i >= j) last = j;
}
}
return lists[0];
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d", node->data);
if (node->next != NULL)
printf("->");
node = node->next;
}
printf("->NULL\n");
}
int main() {
int k, n;
printf("Enter the number of linked lists (K): ");
scanf("%d", &k);
printf("Enter the size of each list (N): ");
scanf("%d", &n);
struct Node* lists[k];
for (int i = 0; i < k; i++) {
printf("list %d: ", i + 1);
lists[i] = NULL;
struct Node* current = NULL;
for (int j = 0; j < n; j++) {
int value;
scanf("%d", &value);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (lists[i] == NULL) {
lists[i] = newNode;
current = newNode;
} else {
current->next = newNode;
current = newNode;
}
}
}
struct Node* result = mergeKSortedLists(lists, k);
printf("Merged Sorted List: ");
printList(result);
return 0;
}
—
Reply to this email directly, view it on GitHub
<#22 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AUNCHW4IVM6YYIT4RY3BNTTYBTPQRAVCNFSM6AAAAAA6N3F36SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBTG43TMNJVGQ>
.
You are receiving this because you authored the thread.Message ID:
<Mozilla-Campus-Club-Cummins/CompetitiveProgramming-HacktoberFest23/issues/22/1783776554
@github.com>
|
Yes did |
tag this issue so it can be merged otherwise it cant be merged
…On Sun, Oct 29, 2023 at 6:14 PM Payalchandak5 ***@***.***> wrote:
Yes did
—
Reply to this email directly, view it on GitHub
<#22 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AUNCHW4ZNMTZULW32BX6USLYBZFTNAVCNFSM6AAAAAA6N3F36SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBUGA4TOMBTGI>
.
You are receiving this because you authored the thread.Message ID:
<Mozilla-Campus-Club-Cummins/CompetitiveProgramming-HacktoberFest23/issues/22/1784097032
@github.com>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given K sorted linked lists of size N each, the task is to merge them all maintaining their sorted order.
Input: K = 3, N = 4
list1 = 1->3->5->7->NULL
list2 = 2->4->6->8->NULL
list3 = 0->9->10->11->NULL
Output: 0->1->2->3->4->5->6->7->8->9->10->11
Input: K = 3, N = 3
list1 = 1->3->7->NULL
list2 = 2->4->8->NULL
list3 = 9->10->11->NULL
Output: 1->2->3->4->7->8->9->10->11
The text was updated successfully, but these errors were encountered: