diff --git a/solutions/0001-0100/0021-merge-two-sorted-lists/README.md b/solutions/0001-0100/0021-merge-two-sorted-lists/README.md new file mode 100644 index 0000000..072b6d7 --- /dev/null +++ b/solutions/0001-0100/0021-merge-two-sorted-lists/README.md @@ -0,0 +1,12 @@ +# 21. Merge Two Sorted Lists + +[LeetCode problem 21](https://leetcode.com/problems/merge-two-sorted-lists/) + +- **Difficulty**: Easy +- **Tags**: linked-list, recursion + +## Description + +Given the heads of two sorted linked lists, merge them into one sorted list +by splicing together the nodes of the two lists, and return the head of the +merged list. diff --git a/solutions/0001-0100/0021-merge-two-sorted-lists/solution.cpp b/solutions/0001-0100/0021-merge-two-sorted-lists/solution.cpp new file mode 100644 index 0000000..b303651 --- /dev/null +++ b/solutions/0001-0100/0021-merge-two-sorted-lists/solution.cpp @@ -0,0 +1,25 @@ +#include "solution.hpp" + +namespace leetcode::p0021 { + +ListNode* solve(ListNode* list1, ListNode* list2) { + ListNode dummy; + ListNode* current = &dummy; + + while (list1 != nullptr && list2 != nullptr) { + if (list1->val <= list2->val) { + current->next = list1; + list1 = list1->next; + } else { + current->next = list2; + list2 = list2->next; + } + current = current->next; + } + + current->next = (list1 != nullptr) ? list1 : list2; + + return dummy.next; +} + +} // namespace leetcode::p0021 diff --git a/solutions/0001-0100/0021-merge-two-sorted-lists/solution.hpp b/solutions/0001-0100/0021-merge-two-sorted-lists/solution.hpp new file mode 100644 index 0000000..9bb0f8a --- /dev/null +++ b/solutions/0001-0100/0021-merge-two-sorted-lists/solution.hpp @@ -0,0 +1,16 @@ +#pragma once + +namespace leetcode::p0021 { + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int value = 0, ListNode* nextNode = nullptr) : val(value), next(nextNode) {} +}; + +// optimized: O(n + m) time, O(1) extra space — splice nodes onto a dummy +// head, iteratively. +ListNode* solve(ListNode* list1, ListNode* list2); + +} // namespace leetcode::p0021 diff --git a/solutions/0001-0100/0021-merge-two-sorted-lists/test.cpp b/solutions/0001-0100/0021-merge-two-sorted-lists/test.cpp new file mode 100644 index 0000000..cfd537f --- /dev/null +++ b/solutions/0001-0100/0021-merge-two-sorted-lists/test.cpp @@ -0,0 +1,34 @@ +#include + +#include + +#include "solution.hpp" + +namespace { + +leetcode::p0021::ListNode* fromVector(const std::vector& values) { + leetcode::p0021::ListNode dummy; + leetcode::p0021::ListNode* current = &dummy; + for (int value : values) { + current->next = new leetcode::p0021::ListNode(value); + current = current->next; + } + return dummy.next; +} + +std::vector toVector(leetcode::p0021::ListNode* head) { + std::vector result; + for (auto* node = head; node != nullptr; node = node->next) { + result.push_back(node->val); + } + return result; +} + +} // namespace + +TEST_CASE("Problem 21", "[p0021]") { + CHECK(toVector(leetcode::p0021::solve(fromVector({1, 2, 4}), fromVector({1, 3, 4}))) == + std::vector{1, 1, 2, 3, 4, 4}); + CHECK(toVector(leetcode::p0021::solve(fromVector({}), fromVector({}))).empty()); + CHECK(toVector(leetcode::p0021::solve(fromVector({}), fromVector({0}))) == std::vector{0}); +} diff --git a/typescript/mergeTwoLists.ts b/typescript/mergeTwoLists.ts deleted file mode 100644 index cff5eed..0000000 --- a/typescript/mergeTwoLists.ts +++ /dev/null @@ -1,31 +0,0 @@ -class ListNode { - val: number - next: ListNode | null - constructor(val?: number, next?: ListNode | null) { - this.val = (val === undefined ? 0 : val) - this.next = (next === undefined ? null : next) - } -} - -function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { - if (!list1) return list2; - if (!list2) return list1; - - const tmp = new ListNode(); - let current = tmp; - - while (list1 && list2) { - if (list1.val <= list2.val) { - current.next = list1; - list1 = list1.next; - } else { - current.next = list2; - list2 = list2.next; - } - current = current.next; - } - - current.next = list1 || list2; - - return tmp.next; -}