diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-07 15:33:17 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-07 15:33:17 +0000 |
commit | 3f0d813778ec254bde98a67ac0550cb455bf6d5a (patch) | |
tree | bf49450844444c323d3d79083776cd74eb20bd96 /2/main.cpp | |
parent | update (diff) | |
download | leetcode-3f0d813778ec254bde98a67ac0550cb455bf6d5a.tar.gz leetcode-3f0d813778ec254bde98a67ac0550cb455bf6d5a.zip |
update
Diffstat (limited to '2/main.cpp')
-rw-r--r-- | 2/main.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/2/main.cpp b/2/main.cpp new file mode 100644 index 0000000..70d8b83 --- /dev/null +++ b/2/main.cpp @@ -0,0 +1,125 @@ + +#include "header.hpp" +#include <algorithm> + +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: + static ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { + int carry = 0; + int sum = 0; + ListNode *head = new ListNode; + ListNode *result = head; + bool l1_last = false; + bool l2_last = false; + while (l1->next != nullptr || l2->next != nullptr) { + + if (l1->next != nullptr) { + sum += l1->val; + l1 = l1->next; + } else { + if (!l1_last) { + sum += l1->val; + l1_last = true; + } + } + + if (l2->next != nullptr) { + sum += l2->val; + l2 = l2->next; + } else { + if (!l2_last) { + sum += l2->val; + l2_last = true; + } + } + + sum += carry; + if (sum >= 10) { + sum -= 10; + carry = 1; + } else { + carry = 0; + } + head->val = sum; + head->next = new ListNode; + head = head->next; + sum = 0; + } + + sum = 0; + if (l1 != nullptr && !l1_last) { + sum += l1->val; + } + if (l2 != nullptr && !l2_last) { + sum += l2->val; + } + sum += carry; + if (sum >= 10) { + sum -= 10; + carry = 1; + } else { + carry = 0; + } + head->val = sum; + if (carry != 0) { + head->next = new ListNode; + head = head->next; + head->val = carry; + } + + return result; + } +}; + +int main(int argc, char **argv) { + ListNode *l11 = new ListNode; + ListNode *l12 = new ListNode; + ListNode *l13 = new ListNode; + ListNode *l14 = new ListNode; + ListNode *l15 = new ListNode; + ListNode *l16 = new ListNode; + ListNode *l17 = new ListNode; + ListNode *l21 = new ListNode; + ListNode *l22 = new ListNode; + ListNode *l23 = new ListNode; + ListNode *l24 = new ListNode; + l11->val = 9; + l12->val = 9; + l13->val = 9; + l14->val = 9; + l15->val = 9; + l16->val = 9; + l17->val = 9; + l21->val = 9; + l22->val = 9; + l23->val = 9; + l24->val = 9; + l11->next = l12; + l12->next = l13; + l13->next = l14; + l14->next = l15; + l15->next = l16; + l16->next = l17; + l17->next = nullptr; + l21->next = l22; + l22->next = l23; + l23->next = l24; + l24->next = nullptr; + + auto result = Solution::addTwoNumbers(l11, l21); + while (result->next != nullptr) { + std::cout << result->val; + result = result->next; + } + std::cout << result->val << "\n"; + + return 0; +} |