Description

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example

Given 7->1->6 + 5->9->2. That is, 617 + 295.

Return 2->1->9. That is 912.

Given 3->1->5 and 5->9->2, return 8->0->8

解题:题目的意思是,给两个链表,倒序表示一个若干位的数。要求返回这两个数的和,并且格式是题中规定的链表,也是倒序。思路很清晰,从头到尾,一位一位地相加,并且用一个数来保存进位。每次相加的时候,都得考虑进位,把进位算在其中。当然,思路越清晰,代码可能看起来就比较笨重。代码如下:

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
// write your code here
ListNode head = new ListNode(0);
ListNode p=head;
ListNode p1 = l1;
ListNode p2 = l2;
int sum = 0;//保存每一位的和
int more=0;//保存进位
while(p1 != null && p2 != null){
sum = p1.val + p2.val+more;
more = sum / 10;
sum = sum % 10;
ListNode temp = new ListNode(sum);
p.next = temp;
p=p.next;
p1 = p1.next;
p2 = p2.next;
}
//如果more不为0
sum = 0;
while(p1 != null){
sum = more + p1.val;
more = sum / 10;
sum = sum % 10;
ListNode temp = new ListNode(sum);
p.next = temp;
p = p.next;
p1 = p1.next;
}
while(p2 != null){
sum = more + p2.val;
more = sum / 10;
sum = sum % 10;
ListNode temp = new ListNode(sum);
p.next = temp;
p = p.next;
p2 = p2.next;
}
if(more != 0){ //如果more不为0,那么最高位就是前面的进位
ListNode temp = new ListNode(more);
p.next = temp;
}
return head.next;
}
}

代码可能有点冗余,曾尝试改进,但发现还是这样写看起来比较清晰。如有错误,欢迎批评指正。

167. Add Two Numbers【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 167. Add Two Numbers【easy】

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

  3. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  4. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  5. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  6. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  7. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  8. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  9. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

随机推荐

  1. shrio的rememberMe不起作用

    在移植项目.每次重启服务器需要登录.比较麻烦.于是研究下shrio的功能. rememberMe大概可以满足我的需求.但是跟着网上配置了.不起作用...... 于是乎查看源代码拉.java的好处... ...

  2. 【Git】Git使用小结

    Git与SVN及TFS这类传统的版本管理的区别: 本地机器也会有分支.代码库的概念 SVN常用的做法是每次写一些代码就提交到仓库,但是Git是先提交到本地(commit),然后当本地有个稳定的版本的时 ...

  3. Maven常用的构建命令

    1.mvn -v 查看maven版本 2.mvn compile 编译项目,生成target文件夹,其中包含编译生成的字节码文件和测试报告.打开cmd,cd到项目的根目录,运行该命令如图所示(如果是第 ...

  4. Oracle 体系结构三 后台进程

    实例后台进程在启动实例时启动,在终止实例时终止运行. SMON SMON(system monitor)起初的任务是安装和打开数据.SMON通过查找和验证数据库控制文件来安装数据库.此后,它通过查找和 ...

  5. 【读书笔记】The Swift Programming Language (Swift 4.0.3)

    素材:Language Guide 初次接触 Swift,建议先看下 A Swift Tour,否则思维转换会很费力,容易卡死或钻牛角尖. 同样是每一章只总结3个自己认为最重要的点.这样挺好!强迫你去 ...

  6. Python记录键盘鼠标敲击次数

    Idea 作为一个每天有一半时间都在电脑旁的人,无时无刻不在敲击着键盘,点击着鼠标.有一天,我突然很想知道在一天的时间内,在我轻盈的指法下面,键盘被我狂敲了多少下,鼠标又被我点击了多少次.甚至更具体一 ...

  7. Docker 相关命令汇总

    操作容器的命令 镜像中的容器启动之后可以在 docker 中操作和查看容器的信息 l   docker ps 查看运行的容器,如果想查看全部加上参数-a 即可 l   docker create 完整 ...

  8. HTML+jq简单轮播图

    .main{    width: 100%;    min-width: 1100px;    display: table;    margin: 0 auto;    text-align: ce ...

  9. Linux awk基础笔记

    正则表达式含义与构成:用单个字符串来描述或者匹配一系列符合某个句法规则的字符串,一般是由普通字符与特殊字符组成 awk 选项 '模式或者命令{编辑指令}' 文件名 awk '//{print}' aw ...

  10. python 基于Anaconda import numpy 报错 Importing the multiarray numpy extension module failed.

    在windows中安装了 Anaconda 运行时报错 原因是系统环境变量起初并没有引入 E:\Tools\Anaconda\Library\bin 解决办法: 在系统环境变量中加入    E:\To ...