问题:

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. POJ1226:Substrings(后缀数组)

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  2. ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work

    ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work &quo ...

  3. 为Eclipse添加Java和Android SDK源代码

    1.添加jdk源码进入eclipse Ctrl + Click -->Attached Source 路径:D:\Program Files\Java\jdk1.8.0_45\src.zip 2 ...

  4. jquery-migrate.js

    这个插件可以用来检测和恢复在jQuery1.9版本中已删除或已过时的API.

  5. dialog横竖屏切换时消失的解决方法

    声明 本文原创,转载请注明来自xiaoQLu http://www.cnblogs.com/xiaoQLu/p/3324764.html dialog的生命周期依赖创建他的activity,怎么设置横 ...

  6. 《Programming with Objective-C》

    苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...

  7. How ADB works

    ADB (Android Debug Bridge): How it works? 2012.2.6 early draft Tetsuyuki Kobayashi What is ADB? If y ...

  8. 【问题与思考】1+"1"=?

    概述 在数学中1+1=2,在程序中1+1=2,而1+"1"=? 围绕着1+"1"的问题,我们来思考下这个问题. 目录: 一.在.Net代码中 二.在JavaSc ...

  9. c++中基类与派生类中隐含的this指针的分析

    先不要看结果,看一下你是否真正了解了this指针? #include<iostream> using namespace std; class Parent{ public: int x; ...

  10. DBA需要掌握的shell知识

    每个中高级DBA都需要掌握一些简单脚本的编写,这样才能从繁杂重复的基础维护工作中解脱出来,才能有时间去研究更有价值的技术.VBird在讲shell script的时候,给出了几个经典的小范例练习,对于 ...