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/0002-add-two-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 2. Add Two Numbers

[LeetCode problem 2](https://leetcode.com/problems/add-two-numbers/)

- **Difficulty**: Medium
- **Tags**: linked-list, math, recursion

## Description

Given two non-empty linked lists representing two non-negative integers, the
digits stored in reverse order, add the two numbers and return the sum as a
linked list in the same reversed-digit format.
29 changes: 29 additions & 0 deletions solutions/0001-0100/0002-add-two-numbers/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "solution.hpp"

namespace leetcode::p0002 {

ListNode* solve(ListNode* l1, ListNode* l2) {
ListNode dummy;
ListNode* current = &dummy;
int carry = 0;

while (l1 != nullptr || l2 != nullptr || carry != 0) {
int sum = carry;
if (l1 != nullptr) {
sum += l1->val;
l1 = l1->next;
}
if (l2 != nullptr) {
sum += l2->val;
l2 = l2->next;
}

carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}

return dummy.next;
}

} // namespace leetcode::p0002
16 changes: 16 additions & 0 deletions solutions/0001-0100/0002-add-two-numbers/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

namespace leetcode::p0002 {

struct ListNode {
int val;
ListNode* next;

explicit ListNode(int value = 0, ListNode* nextNode = nullptr) : val(value), next(nextNode) {}
};

// optimized: O(max(n, m)) time, O(max(n, m)) space — build the result list
// directly while walking both lists and carrying, in one pass.
ListNode* solve(ListNode* l1, ListNode* l2);

} // namespace leetcode::p0002
37 changes: 37 additions & 0 deletions solutions/0001-0100/0002-add-two-numbers/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <vector>

#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

namespace {

leetcode::p0002::ListNode* fromVector(const std::vector<int>& values) {
leetcode::p0002::ListNode dummy;
leetcode::p0002::ListNode* current = &dummy;
for (int value : values) {
current->next = new leetcode::p0002::ListNode(value);
current = current->next;
}
return dummy.next;
}

std::vector<int> toVector(leetcode::p0002::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 2", "[p0002]") {
CHECK(toVector(leetcode::p0002::solve(fromVector({2, 4, 3}), fromVector({5, 6, 4}))) ==
std::vector<int>{7, 0, 8});
CHECK(toVector(leetcode::p0002::solve(fromVector({0}), fromVector({0}))) ==
std::vector<int>{0});
CHECK(toVector(leetcode::p0002::solve(fromVector({9, 9, 9, 9, 9, 9, 9}),
fromVector({9, 9, 9, 9}))) ==
std::vector<int>{8, 9, 9, 9, 0, 0, 0, 1});
}
52 changes: 0 additions & 52 deletions typescript/addTwoNumbers.ts

This file was deleted.

Loading