-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.cpp
More file actions
46 lines (45 loc) · 1 KB
/
AddTwoNumbers.cpp
File metadata and controls
46 lines (45 loc) · 1 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
/**
* 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) {
if (!l1)
return l2;
if (!l2)
return l1;
ListNode dummy(-1);
ListNode *node = &dummy;
ListNode *node1 = l1;
ListNode *node2 = l2;
int c = 0;
while (node1 || node2 || c)
{
int d = c;
if (node1)
{
d += node1->val;
node1 = node1->next;
}
if (node2)
{
d += node2->val;
node2 = node2->next;
}
c = d / 10;
d %= 10;
node->next = new ListNode(d);
node = node->next;
}
return dummy.next;
}
};