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

  题目如上述所示。

  大概翻译:

  给出两个非空的链表代表两个非负的整数。数字以相反的顺序存储,每个节点包含一个数字,将两个数相加并把结果作为一个链表返回。

  你可以假设两个数除了本身是0以外都没有前导0。

  本题最关键的地方在于解决进位问题。

  在网上查询了一些解法,包括借鉴了这篇:http://blog.csdn.net/ljiabin/article/details/40476399

  其中对于进位问题的解决在一开始便引入一个节点以便最后有进位时使用个人觉得有些不妥,并且使代码不太容易懂。

  下面给出我对于此问题的解法:

  【Java代码】

  

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  11. //如果给出就为空,则直接返回另外一个链表
  12. if(l1 == null) return l2;
  13. if(l2 == null) return l1;
  14.  
  15. int flag = 0;//存放进位信息,但是并不是处理最后的进位标志
  16.  
  17. //构造返回结果的第一个节点
  18. ListNode result = new ListNode((l1.val + l2.val) % 10);
  19. ListNode p = result;
  20. flag = (l1.val + l2.val) / 10;
  21.  
  22. l1 = l1.next;
  23. l2 = l2.next;
  24.  
  25. while(l1!=null || l2!=null){
  26. int l1Num = (l1==null)?0:l1.val;//如果l1链表为空,则视值为0
  27. int l2Num = (l2==null)?0:l2.val;//如果l2链表为空,则视值为0
  28. p.next = new ListNode((l1Num + l2Num + flag) % 10);
  29. p = p.next;
  30. flag = (l1Num + l2Num + flag) / 10;
  31. if(l1 != null){
  32. l1 = l1.next;
  33. }
  34. if(l2 != null){
  35. l2 = l2.next;
  36. }
  37.  
  38. }
  39.     //处理最后的进位问题
  40. if(flag != 0){
  41. p.next = new ListNode(flag);
  42. p = p.next;
  43. }
  44.  
  45. return result;
  46. }
  47. }

  

  

如果有任何问题,欢迎跟我联系:xiaomenxiaomen@qq.com

我的github地址:github.com/WXRain

LeetCode---------Add Two Numbers 解法的更多相关文章

  1. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

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

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

  3. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  6. [LeetCode] Add Two Numbers题解

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

  7. [Leetcode] Add two numbers 两数之和

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

  8. [LeetCode] Add Two Numbers

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

  9. LeetCode——Add Two Numbers

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

  10. [LeetCode] Add Two Numbers 链表

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

随机推荐

  1. 前端总结·基础篇·JS(四)异步请求及跨域方案

    前端总结系列 前端总结·基础篇·CSS(一)布局 前端总结·基础篇·CSS(二)视觉 前端总结·基础篇·CSS(三)补充 前端总结·基础篇·JS(一)原型.原型链.构造函数和字符串(String) 前 ...

  2. STM32驱动OV7725摄像头颜色识别

    实验目的: 使用stm32驱动OV7725摄像头进行图像实时采集,在tft屏幕上实时显示并识别图像中的特定颜色,在颜色的周围画上框. 实验现象: 我的工程代码链接: http://download.c ...

  3. IONIC2新建项目并添加导航

    一.   基础搭建 1.      新建IONIC2项目 ionic start myApp tabs --v2 不加--v2会新建ionic1的项目 2.      运行项目 cd myApp io ...

  4. 如何高效实现扫描局域网IP、主机名、MAC和端口

    近几年工作经常使用RFID识读器,智能家居网关,温湿度传感器.串口服务器.视频编码器等,一般是有串口和网口,由于现场原因一般较少使用串口,大多使用网口.连接方法是IP地址和端口,有的设备带搜索软件,有 ...

  5. Unity编译Android的原理解析和apk打包分析

    作者介绍:张坤 最近由于想在Scene的脚本组件中,调用Android的Activity的相关接口,就需要弄明白Scene和Activity的实际对应关系,并对Unity调用Android的部分原理进 ...

  6. angularJS绑定数据中对标签转义的处理

    一.问题 默认情况下,angularJS绑定的数据为字符串文本,不会对其中包含的html标签进行转义生成格式化的文本.在实际工作时碰到接口返回的数据带有html格式时该如何处理. 二.解决办法 1.引 ...

  7. Spring MVC__自定义日期类型转换器

    WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查 ...

  8. .NET遇上Docker - 使用Docker Compose组织Ngnix和.NETCore运行

    本文工具准备: Docker for Windows Visual Studio 2015 与 Visual Studio Tools for Docker 或 Visual Studio 2017 ...

  9. Html 经典布局(二)

    经典布局案例(二): <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  10. 第二章 Oracle数据库应用

    第二章   Oracle数据库应用2.1 表空间和用户权限下管理    2.1.1 表空间        2.1.1.1 分类:            永久性表空间            临时性表空间 ...