文章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; ...
随机推荐
- CSDN博客的一些问题(友好的吐槽)--后记,有一点点改进
近期,CSDN博客真的非常不稳定,时常会出现503错误. 昨天.我发现自己的博客的訪问量仅仅有4万多,今天最终发现它变回原来的6万多了. 我写博客不是为了这个訪问量,可是,CSDN这点使用问题啦. 或 ...
- mahout源码KMeansDriver分析之五CIMapper
接上文重点分析map操作: Vector probabilities = classifier.classify(value.get());// 第一行 Vector selections = pol ...
- 怎样改动SVN的地址
改动svn地址的目的有两个,一个是更改默认svn路径.还有一个就是svn库server迁移了. 我碰到的是另外一种情况,SVN的IP地址改了,须要这么切换: 在本地配置库副本根文件夹点击鼠标右键--& ...
- Effective C++ 条款24
若全部參数皆需类型转换,请为此採用non-member函数 我们直奔主题 假设你定义一个有理数类例如以下 class Rational{ public: Rational(int numerator= ...
- c++多态的案例分析
近期在研究c++中多态的应用 ,当中遇到些许的疑问与问题,可是终于的结果是不容置疑的,以下记录下我的学习过程,以纪念本个知识点. 首先,是从一个案例開始的,题目大意是这种: 设定一个多边形的公共类,然 ...
- SpringMVC @ResponseBody 415错误处理
在查看下面部分内容之前,请先检查你的请求蚕食是否正确,如果全部正确,请继续往下看 刚开始用SpringMVC, 页面要使用jQuery的ajax请求Controller. 但总是失败,主要表现为以下两 ...
- Source not found for StandardEngine(ContainerBase).initInternal() line: 1078
总是这样 在复制完一个项目,并重新起了个名字后. 再打开网页就怎么也打开不了. 第一反应是tomcat出问题了. 于是有了这样的问题: Source not found for StandardE ...
- 一类斜率优化的dp(特有性质:只能连续,不能交叉)
hdu3480 给定一个有n个数的集合,将这个集合分成m个子集,要求子集的并等于全集求花费最小. 花费为该子集的(最大数-最小数)的平方. 我们将n个数排序, a < b < c < ...
- js中escape的用法----前端页面简单加密
escape() 方法,它用于转义不能用明文正确发送的任何字符.比如,电话号码中的空格将被转换成字符 %20,从而能够在 URL 中传递这些字符. http://localhost:8080/a?na ...
- TP-LINK telnet远程 重启路由器(转)
突然断网,以前房东的路由器管理页面可以打开,今天突然间就打不开了.ping了下,可以ping通,于是就想起了房东的路由器是TP-LINK的 可以 telnet登陆的.每次,断网,我都会重启房东的路由器 ...