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 ...
随机推荐
- 【题解】洛谷P2704 [NOI2001] 炮兵阵地(状压DP)
洛谷P2704:https://www.luogu.org/problemnew/show/P2704 思路 这道题一开始以为是什么基于状压的高端算法 没想到只是一道加了一行状态判断的状压DP而已 与 ...
- iOS中的应用启动原理
iOS中的应用启动原理 来源: http://m.blog.csdn.net/article/details?id=50530090 http://m.warting.com/program/2016 ...
- 关于SQL优化这些你了解吗?
目录树 背景 优化点 前提必备知识 优化之一 - 从数据库设计方面考虑 优化之二 - 从SQL语句优化方面考虑 优化之三 - 读写分离与分库分表 背景 在当今这个互联网的时代无非要解决两大难题,其一是 ...
- Kadane算法
Kadane算法用于解决连续子数组最大和问题,我们用ci来表示数组a[0...i]的最大和. 观察可以发现当ci-1 < 0时,ci = ai.用e表示以当前为结束的子数组的最大和,以替代数组c ...
- Drbd双机环境安装配置
一.环境准备 1) 操作系统:ubuntu-14.04.1 x64 2) Ubuntu1 192.168.5.179 /dev/sdb1 主节点 Ubuntu2 192.168.5.178 /dev/ ...
- 编译问题: "ld: duplicate symbol _OBJC_METACLASS_$_XXX..."
在新的SDK环境中调试百度地图的应用程序时,app总是意外退出,找了半天发现错误的原因是unrecognized selector xx的错误,另外还有报了一个Unknown class XXX in ...
- Virtualization state: Optimized (version 7.4 installed)
[Virtualization state: Optimized (version 7.4 installed)] [root@localhost ~]# cd /mnt/ [root@localho ...
- Java性能优化的50个细节
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时 ...
- sea.js模块化工具
sea.js 一. sea.js向全局中引入了两个变量seajs.define: 1.seajs用加载文件 seajs.use(deps,callback)异步引入入口模块 路径要以sea.js文件所 ...
- STM32(9)——通用定时器作为输入捕捉
通用定时器作为输入捕获的使用.我们将用 TIM5 的通道 1 (PA0)来做输入捕获,捕获 PA0 上高电平的脉宽(用 WK_UP 按键输入高电平),通过串口打印高电平脉宽时间 输入捕获简介 输入捕获 ...