Skip to content

Commit

Permalink
Solve merge-two-sorted-lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerold103 committed May 9, 2022
1 parent c278b7f commit f58e900
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 0021-merge-two-sorted-lists/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
https://leetcode.com/problems/merge-two-sorted-lists/

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.



Example 1:


Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:

Input: list1 = [], list2 = []
Output: []
Example 3:

Input: list1 = [], list2 = [0]
Output: [0]


Constraints:

The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both list1 and list2 are sorted in non-decreasing order.
145 changes: 145 additions & 0 deletions 0021-merge-two-sorted-lists/main1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>

struct ListNode {
int val;
struct ListNode *next;
};

struct ListNode *
mergeTwoLists(struct ListNode *list1, struct ListNode *list2)
{
if (list1 == NULL)
return list2;
if (list2 == NULL)
return list1;
struct ListNode *first;
struct ListNode *res;
if (list1->val <= list2->val) {
res = list1;
list1 = list1->next;
} else {
res = list2;
list2 = list2->next;
}
first = res;
while (list1 != NULL && list2 != NULL) {
if (list1->val <= list2->val) {
res->next = list1;
res = list1;
list1 = list1->next;
} else {
res->next = list2;
res = list2;
list2 = list2->next;
}
}
if (list1 != NULL)
res->next = list1;
else
res->next = list2;
return first;
}

static struct ListNode *
make_list(const int *values, int count)
{
if (count == 0)
return NULL;
struct ListNode *nodes = malloc(sizeof(nodes[0]) * count);
for (int i = 0; i < count; ++i) {
nodes[i].val = values[i];
if (i != 0)
nodes[i - 1].next = &nodes[i];
}
nodes[count - 1].next = NULL;
return nodes;
}

static bool
lists_are_eq(const struct ListNode *left, const struct ListNode *right)
{
while (left != NULL && right != NULL) {
if (left->val != right->val)
return false;
left = left->next;
right = right->next;
}
return left == NULL && right == NULL;
}

static inline void
check(bool ok)
{
if (!ok)
abort();
}

int
main()
{
{
struct ListNode *list1 = make_list(
(const int[]){1, 2, 4}, 3);
struct ListNode *list2 = make_list(
(const int[]){1, 3, 4}, 3);
struct ListNode *expected = make_list(
(const int[]){1, 1, 2, 3, 4, 4}, 6);
struct ListNode *actual = mergeTwoLists(list1, list2);
check(lists_are_eq(actual, expected));
free(expected);
free(list1);
free(list2);
}
{
struct ListNode *list1 = make_list((const int[]){}, 0);
struct ListNode *list2 = make_list((const int[]){}, 0);
struct ListNode *expected = make_list((const int[]){}, 0);
struct ListNode *actual = mergeTwoLists(list1, list2);
check(lists_are_eq(actual, expected));
free(expected);
free(list1);
free(list2);
}
{
struct ListNode *list1 = make_list((const int[]){}, 0);
struct ListNode *list2 = make_list((const int[]){0}, 1);
struct ListNode *expected = make_list((const int[]){0}, 1);
struct ListNode *actual = mergeTwoLists(list1, list2);
check(lists_are_eq(actual, expected));
free(expected);
free(list1);
free(list2);
}
{
struct ListNode *list1 = make_list(
(const int[]){3, 5}, 2);
struct ListNode *list2 = make_list(
(const int[]){1, 4, 6, 7}, 4);
struct ListNode *expected = make_list(
(const int[]){1, 3, 4, 5, 6, 7}, 6);
struct ListNode *actual = mergeTwoLists(list1, list2);
check(lists_are_eq(actual, expected));
free(expected);
free(list1);
free(list2);
}
{
struct ListNode *list1 = make_list(
(const int[]){3, 6, 7, 10}, 4);
struct ListNode *list2 = make_list(
(const int[]){-1, 2, 15}, 3);
struct ListNode *expected = make_list(
(const int[]){-1, 2, 3, 6, 7, 10, 15}, 7);
struct ListNode *actual = mergeTwoLists(list1, list2);
check(lists_are_eq(actual, expected));
free(expected);
free(list1);
free(list2);
}
return 0;
}

0 comments on commit f58e900

Please sign in to comment.