Add Two Numbers II Solutions in C++
Number 445
Difficulty Medium
Acceptance 54.6%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers-ii//// Author : liuyubobobo/// Time : 2019-09-21#include <iostream>using namespace std;/// Using reverse/// Time Complexity: O(m + n + max(m, n))/// Space Complexity: O(1)/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {l1 = reverse(l1);l2 = reverse(l2);ListNode* dummyHead = new ListNode(-1), *cur = dummyHead;int carry = 0;for(ListNode* node1 = l1, *node2 = l2; node1 || node2 || carry;node1 = node1 ? node1->next : NULL, node2 = node2 ? node2->next : NULL){int x = node1 ? node1->val : 0;x += node2 ? node2->val : 0;x += carry;cur->next = new ListNode(x % 10);cur = cur->next;carry = x / 10;}return reverse(dummyHead->next);}private:ListNode* reverse(ListNode* node){if(!node->next) return node;ListNode* ret = reverse(node->next);node->next->next = node;node->next = NULL;return ret;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers-ii//// Author : liuyubobobo/// Time : 2019-09-21#include <iostream>#include <stack>using namespace std;/// Using Stack/// Time Complexity: O(m + n + max(m, n))/// Space Complexity: O(m + n)/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {stack<ListNode*> stack1, stack2, stack;ListNode* node = l1;while(node) stack1.push(node), node = node->next;node = l2;while(node) stack2.push(node), node = node->next;int carry = 0;while(!stack1.empty() || !stack2.empty() || carry){int x = 0;if(!stack1.empty()) x += stack1.top()->val, stack1.pop();if(!stack2.empty()) x += stack2.top()->val, stack2.pop();x += carry;stack.push(new ListNode(x % 10));carry = x / 10;}ListNode* ret = stack.top(), *cur = ret;stack.pop();while(!stack.empty())cur->next = stack.top(), cur = cur->next, stack.pop();return ret;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers-ii//// Author : liuyubobobo/// Time : 2019-09-21#include <iostream>#include <stack>using namespace std;/// Recursion/// Time Complexity: O(m + n + max(m, n))/// Space Complexity: O(max(m, n))/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {int len1 = get_length(l1), len2 = get_length(l2);int len = max(len1, len2);if(len1 != len2){ListNode* head = len1 < len2 ? l1 : l2;for(int i = 0; i < len - min(len1, len2); i ++){ListNode* node = new ListNode(0);node->next = head;head = node;}if(len1 < len2) l1 = head;else l2 = head;}int carry = 0;ListNode* res = go(l1, l2, carry);if(carry){ListNode* node = new ListNode(1);node->next = res;res = node;}return res;}private:int get_length(ListNode* head){if(!head) return 0;return 1 + get_length(head->next);}ListNode* go(ListNode* l1, ListNode* l2, int& carry){if(!l1){assert(!l2);carry = 0;return NULL;}int c = 0;ListNode* next = go(l1->next, l2->next, c);int x = l1->val + l2->val + c;ListNode* res = new ListNode(x % 10);res->next = next;carry = x / 10;return res;}};int main() {return 0;}