Mozilla-Campus-Club-Cummins / CompetitiveProgramming-HacktoberFest23

1 stars 25 forks source link

Merge K sorted linked lists #22

Open anu-shka-k opened 10 months ago

anu-shka-k commented 10 months ago

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

Payalchandak5 commented 10 months ago

Please assign me with this issue Payal Chandak UEC2022118 Sy E&tc

anu-shka-k commented 10 months ago

Assigned, please create a PR within 2 days

anu-shka-k commented 10 months ago

Please create PR request not a reply

On Sat, 28 Oct, 2023, 4:20 PM Payalchandak5, @.***> wrote:

include

include

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 https://github.com/Mozilla-Campus-Club-Cummins/CompetitiveProgramming-HacktoberFest23/issues/22#issuecomment-1783776554, 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>

Payalchandak5 commented 10 months ago

Yes did

anu-shka-k commented 10 months ago

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 https://github.com/Mozilla-Campus-Club-Cummins/CompetitiveProgramming-HacktoberFest23/issues/22#issuecomment-1784097032, 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>