Add Two Numbers
This commit is contained in:
parent
63885a6fac
commit
c8b542689c
31
include/s2_add_two_numbers.hpp
Normal file
31
include/s2_add_two_numbers.hpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef S2_ADD_TWO_NUMBERS_HPP
|
||||||
|
#define S2_ADD_TWO_NUMBERS_HPP
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode* next;
|
||||||
|
ListNode() : val(0), next(nullptr) {}
|
||||||
|
ListNode(int x) : val(x), next(nullptr) {}
|
||||||
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Add Two Numbers
|
||||||
|
*
|
||||||
|
* You are given two **non-empty** linked lists representing two non-negative
|
||||||
|
* integers. The digits are stored in **reverse order**, and each of their
|
||||||
|
* nodes contains a single digit. Add the two numbers and return the sum as a
|
||||||
|
* linked list.
|
||||||
|
*
|
||||||
|
* You may assume the two numbers do not contain any leading zero, except the
|
||||||
|
* number 0 itself.
|
||||||
|
*
|
||||||
|
* @param l1 the first linked list
|
||||||
|
* @param l2 the second linked list
|
||||||
|
*/
|
||||||
|
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
23
src/s2_add_two_numbers.cpp
Normal file
23
src/s2_add_two_numbers.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "s2_add_two_numbers.hpp"
|
||||||
|
|
||||||
|
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
|
||||||
|
ListNode *l_begin = new ListNode((l1->val + l2->val) % 10);
|
||||||
|
ListNode *l_end = l_begin;
|
||||||
|
int carry{ (l1->val + l2->val) >= 10 ? 1 : 0 };
|
||||||
|
while (l1->next != nullptr || l2->next != nullptr || carry != 0) {
|
||||||
|
if (l1->next == nullptr && l2->next != nullptr) {
|
||||||
|
l1->next = new ListNode(0);
|
||||||
|
} else if (l1->next != nullptr && l2->next == nullptr) {
|
||||||
|
l2->next = new ListNode(0);
|
||||||
|
} else if (l1->next == nullptr && l2->next == nullptr && carry != 0) {
|
||||||
|
l1->next = new ListNode(0);
|
||||||
|
l2->next = new ListNode(0);
|
||||||
|
}
|
||||||
|
l1 = l1->next;
|
||||||
|
l2 = l2->next;
|
||||||
|
l_end->next = new ListNode((l1->val + l2->val + carry) % 10);
|
||||||
|
l_end = l_end->next;
|
||||||
|
carry = (l1->val + l2->val + carry) >= 10 ? 1 : 0;
|
||||||
|
}
|
||||||
|
return l_begin;
|
||||||
|
}
|
53
tests/s2_add_two_numbers.cpp
Normal file
53
tests/s2_add_two_numbers.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include "s2_add_two_numbers.hpp"
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
TEST(Problem2, Case1) {
|
||||||
|
ListNode *l1 =
|
||||||
|
new ListNode(2,
|
||||||
|
new ListNode(4,
|
||||||
|
new ListNode(3)));
|
||||||
|
ListNode *l2 =
|
||||||
|
new ListNode(5,
|
||||||
|
new ListNode(6,
|
||||||
|
new ListNode(4)));
|
||||||
|
Solution solution;
|
||||||
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
|
EXPECT_EQ(l->val, 7);
|
||||||
|
EXPECT_EQ(l->next->val, 0);
|
||||||
|
EXPECT_EQ(l->next->next->val, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Problem2, Case2) {
|
||||||
|
ListNode *l1 = new ListNode(0);
|
||||||
|
ListNode *l2 = new ListNode(0);
|
||||||
|
Solution solution;
|
||||||
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
|
EXPECT_EQ(l->val, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Problem2, Case3) {
|
||||||
|
ListNode *l1 =
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9)))))));
|
||||||
|
ListNode *l2 =
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9,
|
||||||
|
new ListNode(9))));
|
||||||
|
Solution solution;
|
||||||
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
|
EXPECT_EQ(l->val, 8);
|
||||||
|
EXPECT_EQ(l->next->val, 9);
|
||||||
|
EXPECT_EQ(l->next->next->val, 9);
|
||||||
|
EXPECT_EQ(l->next->next->next->val, 9);
|
||||||
|
EXPECT_EQ(l->next->next->next->next->val, 0);
|
||||||
|
EXPECT_EQ(l->next->next->next->next->next->val, 0);
|
||||||
|
EXPECT_EQ(l->next->next->next->next->next->next->val, 0);
|
||||||
|
EXPECT_EQ(l->next->next->next->next->next->next->next->val, 1);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user