From fd2a8252f47573c2c98fd0997c332da4fe981904 Mon Sep 17 00:00:00 2001 From: BRAGADEESH_V Date: Tue, 31 Oct 2023 22:03:22 +0530 Subject: [PATCH] Problem 23 in leetcode in C language --- leetcode/src/23.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 leetcode/src/23.c diff --git a/leetcode/src/23.c b/leetcode/src/23.c new file mode 100644 index 0000000000..9d49ce641a --- /dev/null +++ b/leetcode/src/23.c @@ -0,0 +1,56 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* mergeKLists(struct ListNode** lists, int listsSize){ +if(listsSize > 1 ){ + + struct ListNode *p1 = lists[0]; + struct ListNode *p2 = NULL; + struct ListNode *third = NULL,*last = NULL; + for(int i=1;ival < p2->val){ + third = last = p1; + p1=p1->next; + last->next = NULL; + }else{ + third = last = p2; + p2=p2->next; + last->next = NULL; + } + while(p1 != NULL && p2!= NULL){ + if(p1->val val){ + last ->next = p1; + last = p1; + p1=p1->next; + last->next = NULL; + }else{ + last ->next = p2; + last = p2; + p2=p2->next; + last->next = NULL; + } + } + + if (p1 ==NULL){ + last ->next = p2; + }else{ + last ->next=p1; + } + p1=third; + } + else if (p1==NULL){ + p1=p2; + }} + return p1; +}else{ + return (listsSize == 0)?NULL:lists[0]; +} + +} \ No newline at end of file