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的更多相关文章

  1. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  2. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  6. [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 ...

  7. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  8. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  9. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

随机推荐

  1. unity3D游戏开发十八之NGUI动画

    我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...

  2. three.js是JavaScript编写的WebGL第 三方库

    three.js是JavaScript编写的WebGL第 三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质 ...

  3. ArrayList线程不安全?

    ArrayList是线程不安全的,轻量级的.如何使ArrayList线程安全? 1.继承Arraylist,然后重写或按需求编写自己的方法,这些方法要写成synchronized,在这些synchro ...

  4. .net数据根据字段进行分类(linq语句)

    var items = List<实体>; var models = items.GroupBy(r => r.分类字段).ToDictionary(d => d.Key, d ...

  5. SAE开发一个应用(不仅仅是建站)

    参考http://jingyan.baidu.com/user/npublic/?un=944615045 http://sae.sina.com.cn/ 激活sae账户 登陆新浪云计算官方网站,网址 ...

  6. 简述负载均衡&CDN技术(转)

    曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需要从无数个角度去考虑他,大到服务器的布局,小到软件中某个文 ...

  7. 足球和oracle列(4):巴西惨败于德国,认为,差额RAC拓扑控制!

    足球与oracle系列(4):从巴西慘败于德国,想到,差异的RAC拓扑对照! 前期回想: 本来想说今晚,回头一想,应该是今早第二场半决赛就要开战了!先来回味一下之前的比赛,本届8支小组赛第一名已经所有 ...

  8. 深入了解回调函数Java

    打回来.我第一次看到Java编程思想,后来Observer模式也适用于一个回调函数的想法.但是,一直没有重视,在处于劣势的最终面试,越来越明白为什么那么多人说Java编程思想,这本书应该是一遍又一遍, ...

  9. Git代理服务器设置和访问Github

    因为现在工作的网络环境有着非常严格的限制,.可以说,在最近的访问通过代理Github它采取了一些曲折的.也积累了一些相关经验.我们认为有必要注意什么. 符合"不要再发明轮子"宗旨, ...

  10. Android 游戏开发 View框架

    按键盘的上下键矩形就会上下移动: 通过实例化Handler对象并重写handkeMessage方法实现了一个消息接收器.然后再线程中通过sendMessage方法发送更新界面的消息,接收器收到更新界面 ...