Add Two Numbers Solutions in C++
Number 2
Difficulty Medium
Acceptance 33.9%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/add-two-numbers/// Author : Hao Chen// Date : 2014-06-18/*** 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 x=0, y=0, carry=0, sum=0;ListNode *h=NULL, **t=&h;while (l1!=NULL || l2!=NULL){x = getValueAndMoveNext(l1);y = getValueAndMoveNext(l2);sum = carry + x + y;ListNode *node = new ListNode(sum%10);*t = node;t = (&node->next);carry = sum/10;}if (carry > 0) {ListNode *node = new ListNode(carry%10);*t = node;}return h;}private:int getValueAndMoveNext(ListNode* &l){int x = 0;if (l != NULL){x = l->val;l = l->next;}return x;}};
C++ solution by pezy/LeetCode
#include <cstddef>#include <cstdlib>struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {ListNode dummy(0), *tail = &dummy;for (div_t sum{0, 0}; sum.quot || l1 || l2; tail = tail->next) {if (l1) { sum.quot += l1->val; l1 = l1->next; }if (l2) { sum.quot += l2->val; l2 = l2->next; }sum = div(sum.quot, 10);tail->next = new ListNode(sum.rem);}return dummy.next;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers/description//// Author : liuyubobobo/// Time : 2018-08-09#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Create new LinkedList for result/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *p1 = l1, *p2 = l2;ListNode *dummyHead = new ListNode(-1);ListNode* cur = dummyHead;int carried = 0;while(p1 || p2 ){int a = p1 ? p1->val : 0;int b = p2 ? p2->val : 0;cur->next = new ListNode((a + b + carried) % 10);carried = (a + b + carried) / 10;cur = cur->next;p1 = p1 ? p1->next : NULL;p2 = p2 ? p2->next : NULL;}cur->next = carried ? new ListNode(1) : NULL;ListNode* ret = dummyHead->next;delete dummyHead;return ret;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers/description//// Author : liuyubobobo/// Time : 2018-08-09#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Using l1 as the result list/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode *p1 = l1, *p2 = l2;ListNode* pre = NULL;int carried = 0;while(p1 || p2){int a = p1 ? p1->val : 0;int b = p2 ? p2->val : 0;if(p1)p1->val = (a + b + carried) % 10;else{pre->next = new ListNode((a + b + carried) % 10);p1 = pre->next;}carried = (a + b + carried) / 10;pre = p1;p1 = p1->next;if(p2) p2 = p2->next;}pre->next = carried ? new ListNode(1) : NULL;return l1;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/add-two-numbers/description//// Author : liuyubobobo/// Time : 2018-08-09#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Using the longest list in l1 and l2 as the result list/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {int len1 = getLen(l1), len2 = getLen(l2);ListNode *p1 = len1 > len2 ? l1 : l2;ListNode *p2 = len1 > len2 ? l2 : l1;ListNode* pre = NULL;int carried = 0;while(p1){int a = p1->val;int b = p2 ? p2->val : 0;p1->val = (a + b + carried) % 10;carried = (a + b + carried) / 10;pre = p1;p1 = p1->next;p2 = p2 ? p2->next : NULL;}pre->next = carried ? new ListNode(1) : NULL;return len1 > len2 ? l1 : l2;}private:int getLen(ListNode* l){int res = 0;while(l)res ++, l = l -> next;return res;}};int main() {return 0;}