167. Add Two Numbers【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 167. Add Two Numbers【easy】
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- 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 ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
随机推荐
- STL Vector使用
http://blog.163.com/zhoumhan_0351/blog/static/399542272010225104536463 Vector 像一个快速的数组,其具有数组的快速索引方式. ...
- 去掉Win7资源管理器左侧不需要的项目
通过修改注册表去掉win7资源管理器左侧你不喜欢的项目: 1,打开注册表WIN+R, 输入:regedit 2,找到HKEY_CLASSES_ROOT \ CLSID \, 再找到对应项, 其包含一个 ...
- react系列(三)组件间通信
组件间通信 React的基本组件元素是一个个组件,组件之间可能存在关联.组合等关系.不同的组件之间,经常会发生数据传递或者交换,我们称之为组件间通信. 根据传递的复杂程度,可以分为三种情况: 父子间通 ...
- Swiper2和Swiper3区别详解与兼容IE8/IE9
最近项目一些网站项目想到用Swiper3来制作响应式,但是发现IE9都不兼容, 而swiper2版本又少一个breakpoints参数 做响应式脚本非常不方便,于是想到新版的浏览器用3 ,iE9和以 ...
- windows搭建本地IIS服务器+php安装+移动设备内网访问服务器
启动IIS服务 1. 打开 “控制面板” => "程序" => "启用或关闭Window功能": 2. 接着勾选相应设置: 3. 继续勾选对应目录下 ...
- chromium之observer_list
典型用法如下 /////////////////////////////////////////////////////////////////////////////// // // OVERVIE ...
- JSP/Servlet开发——第十一章 Ajax交互扩展
1. jQuery实现Ajax的方法: ●除了$.ajax()方法以外,jQuery还提供了其他多种更简单的 Ajax 实现方法,如$.get().$.post().$.getJSON().对象.lo ...
- iOS 清理Xcode项目中没有使用到的图片资源和类文件
接手到一个旧的项目,但是发现里面有太多的无用资源,包括升级app后,一些无用的图片资源并没有被删掉,导致app在打包成ipa包以后,文件变大.手边这个项目IM要更换成环信的IM,之前的一些旧的SDK, ...
- 怎么将oracle的sql文件转换成mysql的sql文件
怎么将sql文件导入PowerDesigner中的方法(将oracle的sql文件转换成mysql的sql文件)呢? 怎么将xx.sql文件的数据库结构导入powerdesigner 的方法呢? 现讲 ...
- PHP Fatal error: Call to undefined function think\finfo_open()
PHP Fatal error: Call to undefined function think\finfo_open() php.ini extension=php_fileinfo. ...