-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Add Two Numbers
58 lines (47 loc) · 2.09 KB
/
2. Add Two Numbers
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
55
56
57
58
-------------------------------------------------------question---------------------------------------------------
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
用两个非空的链表里面的数字组成数相加,返回能够组成和的数字的链表。
------------------------------------------------------solution---------------------------------------------------
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int sum = 0;
ListNode *l3 = NULL;
ListNode **node = &l3;
while(l1!=NULL||l2!=NULL||sum>0)
{
if(l1!=NULL)
{
sum += l1->val;
l1 = l1->next;
}
if(l2!=NULL)
{
sum += l2->val;
l2 = l2->next;
}
(*node) = new ListNode(sum%10);
sum /= 10;
node = &((*node)->next);
}
return l3;
}
};
------------------------------------------------------analyse---------------------------------------------------
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
这道题目得先掌握链表的数据结构,val表示当前值,next只想下一个链表的值,这个类的方法可以看到构造一个返回链表的函数形参是指向
两个链表的首地址结构指针,可以将两个链表传进来。 定义一个整形变量sum, 链表前面的数表示整个数的低位,因此从第一位开始相加并
将两个链表同样位置的数的和赋值给L3链表相对应的值。不过用指向指针的指针来进行复制(*node)表示L3,给L3分配一块空间