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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
给出两个表示两个非负数的链表。 数字以相反的顺序存储,每个结点包含一个数字。 添加两个数字并将其作为链表返回。假设两个数字不包含任何前导零(leading zero),除了数字0本身。
举例:
有两个链表1->5->2 8->5->5
1+8=9,放入输出链表的第一个结点;5+5=10,放入第二个结点,产生进位;进位参与下一个结点的运算:2+5+1=8,放入第三个结点。
解题:
1.引入头结点。返回的要求是第一个结点,那么就需要2个指针,一个一直指向尾结点,一直next移动;一个指向头结点,用于return第一个结点。
2.需要一个进位的标志位carry,赋初始值0,每次两个链表对应结点相加产生的carry为(num1+num2+carry)/10。carry非0即1。
3.2个输入链表长短不一致时,若某一链表当前结点为null,结点数据域赋值0就行了。循环退出条件是两个链表当前结点全是null
4.在退出循环之后要注意判断carry,可能最后一次相加产生了进位。
写法1:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// 头结点
ListNode *head = new ListNode(0), *operNode = head;
// 进位标志
int carry = 0;
while (l1 != NULL || l2 != NULL) {
int num1 = l1 == NULL ? 0 : l1->val;
int num2 = l2 == NULL ? 0 : l2->val;
int sum = num1 + num2 + carry;
operNode->next = new ListNode(sum % 10);
carry = sum / 10;
operNode = operNode->next;
l1 = l1 == NULL ? NULL : l1->next;
l2 = l2 == NULL ? NULL : l2->next;
}
// 最后一次相加之后判断标志位
if (carry == 1) {
operNode->next = new ListNode(1);
}
return head->next;
}
};
写法2:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL) return l1;
if (l2 == NULL) return l2;
int carry = 0;//进位标志
ListNode *head = new ListNode(0);//头结点
ListNode *p = head;//移动的指针
while (l1!=NULL ||l2!=NULL)
{
ListNode *node = new ListNode(0);
int num_l1 = 0;
if (l1 != NULL){
num_l1 = l1->val;
l1 = l1->next;
}
int num_l2 = 0;
if (l2 != NULL){
num_l2 = l2->val;
l2 = l2->next;
}
node->val = (num_l1 + num_l2 + carry) % 10;
carry = (num_l1 + num_l2 + carry) / 10;
p->next = node;
p = node;
}
if (carry)
{
ListNode *node = new ListNode(0);
node->val = carry;
p->next = node;
}
return head->next;
}
};
写法3:附链表创建、测试用例。
STL 有一个专门的结构体叫做 div_t, 其包含两个成员,分别是 quot(quotient) 与 rem(remainder)。quot是商,rem是余数,比如div_t result = div(28, 5);
,那么result.quot=5,result.rem=3。
在这道题里,quot可以存放carry位,rem可以作为结点数据域。
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *head = new ListNode(0), *tail = head;
div_t sum{ 0,0 };
for (; 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);
}
if (sum.quot)
{
tail->next = new ListNode(sum.quot);
}
return head->next;
}
};
ListNode *createList(initializer_list<int> list) {
ListNode *head = new ListNode(0), *cur = head;
if (list.size())
{
for (auto beg = list.begin(); beg != list.end(); ++beg)
{
cur->next = new ListNode(*beg);
cur = cur->next;
}
}
return head->next;
}
int main()
{
Solution s;
ListNode *l1 = createList({ 1,5,2 });
ListNode *l2 = createList({ 2,5,2 });
ListNode *result = s.addTwoNumbers(l1, l2);
for (ListNode *cur = result; cur != NULL; cur = cur->next)
{
cout << cur->val << "->";
}
return 0;
}