文章13称号 Add Two Numbers
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Solution1:不用分配多余的空间,可是会改变原先lists中的值。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1==null&&l2==null) return null;
if(l1==null) return l2;
if(l2==null) return l1;
ListNode ptr1 = l1, ptr2 = l2;
int c = 0;
ListNode prev=l1;//reserve the previous value in the generated list
while(ptr1!=null && ptr2!=null){
ptr1.val = ptr1.val+ptr2.val+c;
c = ptr1.val/10;
ptr1.val = ptr1.val%10;
prev = ptr1;
ptr1=ptr1.next;
ptr2=ptr2.next;
}
if(ptr2!=null) { //ptr1==null
prev.next = ptr2;
ptr1 = ptr2;
ptr2=null; //must do this for maintaining end condition }
while(c!=0&&ptr1!=null){
ptr1.val =ptr1.val+c;
c = ptr1.val/10;
ptr1.val = ptr1.val%10;
prev = ptr1;
ptr1 = ptr1.next;
} if(c!=0&&ptr1==null&&ptr2==null) {
ListNode newNode =new ListNode(1);
prev.next = newNode;
ptr1 = newNode;
}
return l1;
}
}
Solution 2: 不会改变原lists中的值。但需建立一个新list
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null&&l2==null) return null;
if(l1==null) return l2;
if(l2==null) return l1;
ListNode head = new ListNode(0);
int c = 0;
ListNode prev=head;//reserve the previous value in the generated list
while(l1!=null || l2!=null){
ListNode cur = new ListNode(0);
if(l1!=null){
cur.val += l1.val;
l1=l1.next;
}
if(l2!=null){
cur.val += l2.val;
l2=l2.next;
}
cur.val += c;
c = cur.val/10;
cur.val = cur.val%10;
prev.next = cur;
prev = cur;
}
if(c!=0) {
ListNode newNode =new ListNode(1);
prev.next = newNode;
}
return head.next;
}
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
文章13称号 Add Two Numbers的更多相关文章
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode_2] Add Two Numbers
LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...
随机推荐
- cocos2d_x_05_Box2D物理引擎
一.认识Box2D 帮助文档,共69页 二.创建一个物理世界 先导入主头文件 #include <Box2D/Box2D.h> 三.物理世界一览 像素转成米 的比例因子 就是32 三.运动 ...
- 已知直线上的两点 A(x1, y1), B(x2, y2) 和另外一点 C(x0, y0),求C点到直线的距离。
数学知识太差,一点点积累,高手勿喷. 1. 先求出AB向量 a = ( x2-x1, y2-y1 ) 2. 求AB向量的单位方向向量 b = √((x2-x1)^2 + (y2-y1)^2)) a1 ...
- hdu2036 (计算多边形的面积)
Input 输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1 ...
- poj 3265 Problem Solving dp
这个题目容易让人误以为是贪心就可以解决了,但是细想一下很容易举出反例. dp[i][j]表示解决了i个问题,最后一个月解决的问题数目. #include <iostream> #inclu ...
- C++设计模式之建造者模式(三)
4.引入钩子方法的建造者模式 建造者模式除了逐步构建一个复杂产品对象外.还能够通过Director类来更加精细地控制产品的创建过程.比如添加一类称之为钩子方法(HookMethod)的特殊方法来控制是 ...
- JAVA的class打包成dll
一.将已经编译后的java中Class文件进行打包:打包命令JAR 如:将某目录下的所有class文件夹全部进行打包处理: 使用的命令:jar cvf test.jar -C com/ . //注意这 ...
- Vs2012于Linux应用程序开发(4):公共财产的定义
在嵌入式开发流程.有些参数基本上不改变,比如编译主机IP,username,password等参数.我们用VS提供的属性管理器来保存这些參数. 打开属性管理器: watermark/2/text/aH ...
- 关于LIST.Select().ToList()慢的问题
var sendlist = emailList.Select(email => new MailMessage { MailServer = SMTPServer, UserName = Se ...
- C#区域截图——调用API截图
原文:C#区域截图——调用API截图 前言:截图对于一个C++开发者来说无非是小菜一碟,也有朋友使用C#的 Graphics.CopyFromScreen 方法屏幕操作,作为一名整天想着用 C++ 开 ...
- Thinkpad X200 屏幕备案
妈妈蛋,屏幕废物前几天(闪屏->暗->变暗),因此,它只能监视房外 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk2NTg5MA= ...