forked from livingstream/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.cpp
More file actions
50 lines (47 loc) · 1.38 KB
/
AddTwoNumbers.cpp
File metadata and controls
50 lines (47 loc) · 1.38 KB
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
//============================================================================
// You are given two linked lists representing two non-negative numbers.
// The digits are stored in reverse order and each of their nodes contain a
// single digit. Add the two numbers and return it as a linked list.
//
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8
//============================================================================
#include <cstdlib>
/**
* 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) {
ListNode *l3 = NULL, *node = NULL;
int c = 0;
while (true) {
int a, b;
if (l1 != NULL) {
a = l1->val;
l1 = l1->next;
}
else a = 0;
if (l2 != NULL) {
b = l2->val;
l2 = l2->next;
}
else b = 0;
int s = a + b + c;
c = s / 10;
if (l3 == NULL) l3 = node = new ListNode(s % 10);
else node->next = new ListNode(s % 10), node = node->next;
if (l1 == NULL && l2 == NULL) break;
}
if (c == 1) node->next = new ListNode(1);
return l3;
}
};
int main() {
return 0;
}