问题:

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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

官方难度:

Medium

翻译:

现有2个链表结构,每个节点存储非负整数,每个结点的数字都是个位数且倒序排列,将这2个链表代表的数字相加。

例子:

链表一:2 -> 4 -> 3

链表二:5 -> 6 -> 4

输出链表:7 -> 0 -> 8

额外例子:

链表一:1 -> 5

链表二:8 -> 5

输出链表:9 -> 0 -> 1

  1. 结点定义已经给了,是单向链表的结点定义:

         private static class ListNode {
    int val;
    ListNode next; ListNode(int x) {
    val = x;
    }
    }

    ListNode

  2. 最初题目给的例子,其实题意并不是特别清晰,一开始我的理解其实是存在问题的。其实题目的要求是(以额外例子为例):1+8=9,放入输出链表的第一个结点;5+5=10,放入第二个结点,产生进位;1放入第三个结点。
  3. 分析一下题目的要求,返回的要求是第一个结点,那么就需要2个指针,一个用来操作,一直next,一个始终指向第一个结点,用于return。
  4. 需要一个进位的标志位carry,而且没有必要用boolean型的,用int型的更好,可以初始值赋值0,下一次的carry=(num1+num2+carry)/10。
  5. 因为可能出现2个输入链表长短不一致的情况,当前结点为null时,赋值0就行了。
  6. 引入头结点的概念,因为初始的结点需要赋值,如果初始化为第一个结点的时候,会把大部分循环里的事情先拉出来做一遍,这样代码就会显得很繁琐,引入头结点可以避免这种情况,return的时候记得head.next。
  7. 循环退出条件是两个链表当前结点全是null,在退出循环之后需要判断进位的标志位carry,因为可能会多产出一位。
  8. 对方法进行入参检查是一个好习惯,但是本方法即使没有入参检查(输入2个null),也不会出错,只会返回null,那就不必写了。

解题代码:

     public static 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;
}

addTwoNumbers

备注:

    1.  鉴于之后有比较多的链表结构的问题,不妨自己实现一下链表。

相关链接:

https://leetcode.com/problems/add-two-numbers/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q002.java

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/structure/SinglyLinkedList.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.002:Add Two Numbers的更多相关文章

  1. LeetCode-2: Add Two Numbers

    [Problem:2-Add Two Numbers] You are given two non-empty linked lists representing two non-negative i ...

  2. Q2:Add Two Numbers

    2. Add Two Numbers 官方的链接:2. Add Two Numbers Description : You are given two non-empty linked lists r ...

  3. LeetCode4:Add Two Numbers

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

  4. leetcode:Add Two Numbers

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

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

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

  6. LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium

    题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...

  7. LeetCode OJ:Add Two Numbers (相加链表之数)

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

  8. 面试题:Add Two Numbers(模拟单链表)

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

  9. LeetCode第二题:Add Two Numbers

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

随机推荐

  1. Eclipse调试时Application XXX is waiting for the debugger to attach的提示

    原文链接: http://blog.csdn.net/star_huang/article/details/7678845 最近Eclipse调试时总是出现Application XXX  is wa ...

  2. EvreryDay Collect

    1.在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService? 在System.Net中提供了一个NetworkCredential,只有获得该凭证的用户才能访问相 ...

  3. [初读笔记] Cloud Migration Research: A Systematic Review (TCC, 2013)

    Pooyan Jamshidi, Aakash Ahmad, Claus Pahl, "Cloud Migration Research: A Systematic Review," ...

  4. cocos2dx的lua绑定

    一.cocos2dx对tolua++绑定的修正 A.c对lua回调函数的引用 在使用cocos2dx编写游戏时,我们经常会设置一些回调函数(时钟.菜单选择等).如果采用脚本方式编写游戏的话,这些回调函 ...

  5. node.js中log4js的使用

    以前用过forever进程守护的日志记录到指定文件,但是只能保存到一个文件中不能分片,这样到只日志文件越来越大, forever start -s -l ./forever.log app.js -l ...

  6. NetBPM的安装 -转

    NetBPM的安装还是比较简单的,有比较详细的文档. 1.当然是先下载运行程序了, netbpm-0.8.3.1.zip ,官方网站:http://www.netbpm.org:2.然后解压后自己看 ...

  7. Ping-Pong (Easy Version)(DFS)

    B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. access里like的通配符不能用%,要用*

    转自http://www.knowsky.com/339881.html access里like的通配符用法是这样:     “?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数 ...

  9. java攻城师之路--复习java web之jsp入门_El表达式_JSTL标签库

    JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...

  10. iOS开发中一些常见的并行处理(转)

    本文主要探讨一些常用多任务的最佳实践.包括Core Data的多线程访问,UI的并行绘制,异步网络请求以及一些在运行态内存吃紧的情况下处理大文件的方案等.

其实编写异步处理的程序有很多坑!所以,本文 ...