题目原文:

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

题意解析:

给定两个链表表示两个非负数。数字逆序存储,每一个节点包括一个单一的数字。计算两个链表表示的数的和。并以相同格式的链表的形式返还结果。

解法就是直接操作链表相加就能够了。。

代码例如以下:

C++

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * ans = NULL, *last = NULL;
int up = 0;
while (NULL != l1 && NULL != l2) {
int tmp = l1->val + l2->val + up;
up = tmp / 10;
if (NULL == last) {
ans = new ListNode(tmp % 10);
last = ans;
} else
last = pushBack(last, tmp % 10);
l1 = l1->next;
l2 = l2->next;
}
while (NULL != l1) {
int tmp = l1->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l1 = l1->next;
}
while (NULL != l2) {
int tmp = l2->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l2 = l2->next;
}
if (0 != up) {
ListNode * l = new ListNode(up);
last->next = l;
}
return ans;
} ListNode * pushBack(ListNode * last, int val) {
ListNode * l = new ListNode(val);
last->next = l;
return l;
}
};

Python

# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0; head = ListNode(0); curr = head;
while l1 and l2:
Sum = l1.val + l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; l2 = l2.next; curr = curr.next
while l1:
Sum = l1.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; curr = curr.next
while l2:
Sum = l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l2 = l2.next; curr = curr.next
if carry > 0:
curr.next = ListNode(carry)
return head.next

水一枚。。。。

LeetCode第四题,Add Two Numbers的更多相关文章

  1. leetcode第四题--Add Two Numbers

    Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  2. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  3. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  4. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  5. LeetCode解题笔记 - 2. Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  6. LeetCode第二题—— Add Two Numbers(模拟两数相加)

    Description: You are given two non-empty linked lists representing two non-negative integers. The di ...

  7. [算法题] Add Two Numbers

    题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

随机推荐

  1. JenKins 环境搭建 for Centos6.5

    1,JenKines简单介绍--图解

  2. mongodb use db show dbs

    mongodb 常用命令: 在dbs间切换用 use xxxdb 之后再操作就是只针对 xxxdb了: show dbs显示全部数据库 show collections 显示全部集合 mongodb数 ...

  3. php使用NuSoap调用java/C# webservice乱码问题

    今天调用了一个 NuSoap 的接口程序,一切流程操作都很正常,就是最后接收返回值的时候出现了乱码问题(我这边是做一个越南项目固然返回越南语,不过认为中文应该同样实用,需要的人可以尝试下)   许多使 ...

  4. 280行代码:Javascript 写的2048游戏

    2048 原作者就是用Js写的,一直想尝试,但久久未动手. 昨天教学生学习JS代码.最好还是就做个有趣的游戏好了.2048这么火,是一个不错的选择. 思路: 1. 数组 ,2维数组4x4 2. 移动算 ...

  5. HDU1506_Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. php缓存生成及更新实现参考哦

    <?php //如果在find/findAll里传入了参数,则该参数即为key ORM::factory('article')->where('user_id', '=', '2')-&g ...

  7. 关于C语言中有string类型吗?

    一.问题来源 今天在VS2010平台上,尝试采用scanf() string word; scanf("%s",&word); 然后发现错误,输出采用 printf(&qu ...

  8. google base 之MessagePumpForUI

    base库中比较有意思就是这个类了,如同很多界面库一样,创建了一个隐藏窗口来处理需要在界面线程处理的消息,大体原理也就是需要执行task的时候发送一个自定义的消息,当窗口接收到task的时候调用保存起 ...

  9. –save与–save-dev

    使用npm install node_module –save自动更新dependencies字段值: 使用npm install node_module –save-dev自动更新devDepend ...

  10. markdown流程图

    markdown流程图 markdown流程图 markdown流程图语法:https://github.com/adrai/flowchart.js 定义元素阶段的语法是 tag=>type: ...