forked from upupming/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.add-two-numbers.cpp
54 lines (48 loc) · 1.34 KB
/
2.add-two-numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode id=2 lang=cpp
*
* [2] Add Two Numbers
*/
// @lc code=start
/**
* Definition for singly-linked list.
* 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:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *root = new ListNode,
*l1Next = l1 ? l1->next : nullptr,
*l2Next = l2 ? l2->next : nullptr,
*tmp;
bool someNull = l1 == nullptr || l2 == nullptr;
tmp = (l1 == nullptr ? l2 : l1);
int sum = someNull ? tmp->val : l1->val + l2->val;
root->val = sum % 10;
int c = sum / 10;
// l1 and l2 both continues
if (l1Next && l2Next) {
l1Next->val += c;
root->next = addTwoNumbers(l1Next, l2Next);
}
// l1 and l2 both ends
else if (!l1Next && !l2Next) {
if (c > 0)
root->next = new ListNode(c);
}
// one of l1, l2 ends
else {
tmp = (l1Next == nullptr ? l2Next : l1Next);
tmp->val += c;
root->next = addTwoNumbers(tmp, nullptr);
}
return root;
}
};
// @lc code=end