Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions solutions/0001-0100/0021-merge-two-sorted-lists/README.md
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions solutions/0001-0100/0021-merge-two-sorted-lists/solution.cpp
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions solutions/0001-0100/0021-merge-two-sorted-lists/solution.hpp
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions solutions/0001-0100/0021-merge-two-sorted-lists/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <vector>

#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

namespace {

leetcode::p0021::ListNode* fromVector(const std::vector<int>& 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<int> toVector(leetcode::p0021::ListNode* head) {
std::vector<int> 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<int>{1, 1, 2, 3, 4, 4});
CHECK(toVector(leetcode::p0021::solve(fromVector({}), fromVector({}))).empty());
CHECK(toVector(leetcode::p0021::solve(fromVector({}), fromVector({0}))) == std::vector<int>{0});
}
31 changes: 0 additions & 31 deletions typescript/mergeTwoLists.ts

This file was deleted.

Loading