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. 【题解】洛谷P3959 [NOIP2017TG] 宝藏(状压DP+DFS)

    洛谷P3959:https://www.luogu.org/problemnew/show/P3959 前言 NOIP2017时还很弱(现在也很弱 看出来是DP 但是并不会状压DP 现在看来思路并不复 ...

  2. Unity3D Errors And Fix

    Author Error: Shader warning in 'Custom/ShowAnimation': Not enough temporary registers, needs 9 (com ...

  3. 四种常见的 POST 提交数据方式对应的content-type

    原文地址:https://www.cnblogs.com/wushifeng/p/6707248.html application/x-www-form-urlencoded 这应该是最常见的 POS ...

  4. Jquery与js简单对比

    //Javascript window.onload=function () { var oBtn=document.getElementById('btn1'); oBtn.onclick=func ...

  5. JQuery制作网页——第八章 使用jQuery操作DOM

    1.DOM操作: DOM操作分为三类: ●DOM Core:任何一种支持DOM的编程语言都可以使用它,如getElementById().getElementsByName: ●HTML-DOM:用于 ...

  6. mongodb查看数据库和表的信息

    mongodb查看数据库和表的方法比较简单,在为这里推荐使用stats的方法,直观并且详细. 1.查看数据库 db.stats();1输出: { "db" : "siri ...

  7. Redis集群进阶之路

    Redis集群规范 本文档基于Redis 3.X或更高版本,讲解Redis集群算法以及设计原理.此官方文档长期更新且随着Redis新版本特性的变化变动,详细请留意官网. 官网地址:https://re ...

  8. thinkphp3.2.3 HTML 页面跳转

    1.   http://域名/index.php(入口文件)/模块/控制器/方法 2.{:U('控制器/方法')}

  9. try catch finally 中 returne的执行顺序

    结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  10. centos7安装mysql5.7.18笔记

    重装了一下系统,装了centos7,但是centos7下默认没有安装mysql,有MariaDB数据库,网上的解释是: “MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用 ...