[LeetCode] Add Two Numbers 两个数字相加

 

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.

这道并不是什么难题,算法很简单,链表的数据类型也不难,就是建立一个新链表,然后把输入的两个链表从头往后撸,每两个相加,添加一个新节点到新链表后面。为了避免两个输入链表同时为空,我们建立一个dummy结点,将两个结点相加生成的新结点按顺序加到dummy结点之后,由于dummy结点本身不能变,所以我们用一个指针cur来指向新链表的最后一个结点。好,可以开始让两个链表相加了,这道题好就好在最低位在链表的开头,所以我们可以在遍历链表的同时按从低到高的顺序直接相加。while循环的条件两个链表中只要有一个不为空行,由于链表可能为空,所以我们在取当前结点值的时候,先判断一下,若为空则取0,否则取结点值。然后把两个结点值相加,同时还要加上进位carry。然后更新carry,直接 sum/10 即可,然后以 sum%10 为值建立一个新结点,连到cur后面,然后cur移动到下一个结点。之后再更新两个结点,若存在,则指向下一个位置。while循环退出之后,最高位的进位问题要最后特殊处理一下,若carry为1,则再建一个值为1的结点,代码如下:

C++ 解法:

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(-1), *cur = dummy;
int carry = 0;
while (l1 || l2) {
int val1 = l1 ? l1->val : 0;
int val2 = l2 ? l2->val : 0;
int sum = val1 + val2 + carry;
carry = sum / 10;
cur->next = new ListNode(sum % 10);
cur = cur->next;
if (l1) l1 = l1->next;
if (l2) l2 = l2->next;
}
if (carry) cur->next = new ListNode(1);
return dummy->next;
}
};

Java 解法:

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
int carry = 0;
while (l1 != null || l2 != null) {
int d1 = l1 == null ? 0 : l1.val;
int d2 = l2 == null ? 0 : l2.val;
int sum = d1 + d2 + carry;
carry = sum >= 10 ? 1 : 0;
cur.next = new ListNode(sum % 10);
cur = cur.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
if (carry == 1) cur.next = new ListNode(1);
return dummy.next;
}
}

[LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现的更多相关文章

  1. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  2. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  4. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  5. LeetCode 2. Add Two Numbers (两数相加)

    题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...

  6. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  7. [leetcode]2. Add Two Numbers两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  8. [LeetCode]2.Add Two Numbers 两数相加(Java)

    原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...

  9. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

随机推荐

  1. 杂项-快捷键:Google浏览器常用快捷键

    ylbtech-杂项-快捷键:Google浏览器常用快捷键 1.返回顶部 1. Google浏览器,是一个由Google(谷歌)公司开发的开放原始码网页浏览器.该浏览器是基于其他开放原始码软件所撰写, ...

  2. 如何让MP4 video视频背景色变成透明?

    本文转自:https://www.zhangxinxu.com/wordpress/2019/05/mp4-video-background-transparent/ 亲测,pc端有效,但移动端微信内 ...

  3. jQuery进度条设置

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...

  4. Spark SQL 编程API入门系列之Spark SQL的作用与使用方式

    不多说,直接上干货! Spark程序中使用SparkSQL 轻松读取数据并使用SQL 查询,同时还能把这一过程和普通的Python/Java/Scala 程序代码结合在一起. CLI---Spark ...

  5. 解析RecyclerView(2)——带顶部View和底部View的RecyclerView

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  6. ifame子页实现父页面刷新(或跳转到指定页面)

    <script>parent.location.replace('../D_DailyManager/Add.aspx?id=" + x + "');</scri ...

  7. ASP.NET在IIS 5/6上的运行模型(ISAPI)

    IIS 5.X中的ASP.NET 实现了Web Server和ASP.NET App的分离. IIS作为Web Server运行在InetInfo.exe进程上.该进程是非托管的本地进程. ASP.N ...

  8. IBM 总架构师:话说程序员的职业生涯

    作者:IBM 软件集团大中华区总架构师 寇卫东 有一些年轻的程序员向我咨询,将来的路应该怎么走?俗话说,条条大路通罗马.不同的路都能走向成功.到底选哪条路,取决于自己的兴趣.可能有程序员会问:如果还没 ...

  9. Oracle安装后命令行中运行sqlplus / as sysdba出现错误ora-01031:insufficient privileges

    Win10安装Oracle后命令行中运行sqlplus as sysdba出现错误ora-01031insufficient privileges的解决方法 情景描述 错误样例 错误分析 解决方法 情 ...

  10. JAVA 静态内部类--转自http://bbs.csdn.net/topics/350021609

    内部类其实并不是非要声明成static的..主要还是要看实际情况决定..静态和非静态有不同的作用.. 引用一篇文章给楼主参考下吧. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的( ...