1、题目:
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.
Examples:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.代码框架:
/**
* 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) {
......
}
};
2、代码:
// 版本一,我的代码,65ms,20180317
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* l3 = new ListNode(0);
ListNode* current = l3;
while (l1 != NULL && l2 != NULL)
{
current->val += l1->val + l2->val;
if (current->val < 10)
{
if (l1->next != NULL || l2->next != NULL)
{
current->next = new ListNode(0);
current = current->next;
}
}
else
{
current->val = current->val - 10;
current->next = new ListNode(1);
current = current->next;
}
l1 = l1->next;
l2 = l2->next;
}
if (l1 == NULL)
{
while (l2 != NULL)
{
current->val += l2->val;
if (current->val < 10)
{
if (l2->next != NULL)
{
current->next = new ListNode(0);
current = current->next;
}
}
else
{
current->val = current->val - 10;
current->next = new ListNode(1);
current = current->next;
}
l2 = l2->next;
}
}
if (l2 == NULL)
{
while (l1 != NULL)
{
current->val += l1->val;
if (current->val < 10)
{
if (l1->next != NULL)
{
current->next = new ListNode(0);
current = current->next;
}
}
else
{
current->val = current->val - 10;
current->next = new ListNode(1);
current = current->next;
}
l1 = l1->next;
}
}
return l3;
}
};
解析:最自然的想法,但过于复杂。记住每次新建一个next节点都需要执行new语句。记住用一个current去递归,而不用l3去做。
// 版本二,69ms,标准答案
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *dummyHead = new ListNode(0);
ListNode *p = l1, *q = l2, *curr = dummyHead;
int carry = 0;
while (p != NULL || q != NULL) {
int x = (p != NULL) ? p->val : 0;
int y = (q != NULL) ? q->val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr->next = new ListNode(sum % 10);
curr = curr->next;
if (p != NULL) p = p->next;
if (q != NULL) q = q->next;
}
if (carry > 0) {
curr->next = new ListNode(carry);
}
return dummyHead->next;
}
};
解析:将大于10的判断直接用/10和%10取代了。将链表是否为空的判断用一个选择表达式取代了,当一个链表为空时,其所代表的值为0。
// 版本三,59ms,别人答案
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(0), *p = &preHead;
int extra = 0;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;
extra = sum / 10;
p->next = new ListNode(sum % 10);
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
};
解析:最为简洁的方法。
// 版本四,59ms,别人答案
class Solution {
public:
ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *c1 = l1;
ListNode *c2 = l2;
ListNode *sentinel = new ListNode(0);
ListNode *d = sentinel;
int sum = 0;
while (c1 != NULL || c2 != NULL) {
sum /= 10;
if (c1 != NULL) {
sum += c1->val;
c1 = c1->next;
}
if (c2 != NULL) {
sum += c2->val;
c2 = c2->next;
}
d->next = new ListNode(sum % 10);
d = d->next;
}
if (sum / 10 == 1)
d->next = new ListNode(1);
return sentinel->next;
}
};
3、VS上实现的完整代码(可调试)
// Leetcode Problem 2 Add two numbers.
#include <iostream>
using namespace std;
// Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// 版本三,59ms,别人答案
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(0), *p = &preHead;
int extra = 0;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;
extra = sum / 10;
p->next = new ListNode(sum % 10);
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
};
void main()
{
Solution *s = new Solution();
ListNode *l1=new ListNode(2);
l1->next = new ListNode(4);
l1->next->next = new ListNode(3);
ListNode *l2 = new ListNode(5);
l2->next = new ListNode(6);
l2->next->next = new ListNode(4);
ListNode *l3 = s->addTwoNumbers(l1, l2);
}