文章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; ...
随机推荐
- 事务的使用示例及WinForm实现中的若干问题
--事务的使用示例 create database MyDB go use MyDB create table account ( Id int identity primary key, balan ...
- mysql联合索引的应用
有一个log表,结构是这样的: CREATE TABLE `weblog` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `ip` varc ...
- 登录oracle时,scott is locked (帐户被锁定) 的解决方法
登录Oracle时,用scott/tiger 通常此时会报一个错误: scott is locked (帐户被锁定) 现在就要用超级用户system将scott帐户进行解锁. cmd->sql ...
- C++用于修饰的keyword
1.const修饰指针 (1)const int *p=&x:不可改动*p的内容,但能够让指针p指向还有一个地址,和int const *p一样 (2)int *const p=&x: ...
- android--自己定义ProgressDialog显示位置(其他Dialog子类都能够设置)
1.普通情况下,系统默认的Dialog显示位置为屏幕居中: pbDialog = new ProgressDialog(MainActivity.this); pbDialog.setMessage( ...
- 教你怎么去一个APP的JSON数据,你懂的
今天闲着没事.谁让我今天是光棍节呢,算给大家一个福利.没事逛着应用市场.想找个应用高仿下,突然发现一个应用竟然跟我一个名字尼玛,尼玛应用界面做的非常easy.我认为应该不难. 惯性操作想去破解APK. ...
- swfobject.js的简单配置
因为工作需要在网页中迁入flash,开发过程中,发现直接使用embed自己开发的话需要考虑各种兼容性,也比较麻烦, 网上也找了几个相关的插件,比较使用之下,发现swfobject.js这一款还是蛮不错 ...
- 完美去除WPF按钮的边框
主页面背影图片, 添加5个功能按钮,并设置按钮的Background和BorderBrush为Transparent,好像没有问题,运行效果 不仅有一个发光的边框,而且当鼠标经过时,按钮就不在透明, ...
- java中取得上下文路径的方法
1.request.getContextPath(); 获得web根的上下文环境 如 /tree tree是web项目的root context 2.可以在servlet的init方法里 String ...
- flex4 amcharts 删除水印
package com.twobowl.component.manualTopo { import com.amcharts.AmPieChart; import com.amcharts.chart ...