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. hdu 5282 Senior's String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  2. 在VS2012中使用GitHub

    注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(Test) *创建完成后,记录该仓库的地址:https://github.com/ ...

  3. ps、top 、free查看用户资源信息

      查看root用户的进程信息. 运行命令: ps -u root 查看oracle用户的进程信息. 运行命令: ps -u oracle 若查看现在的资源占用情况,如何呢? 运行命令: top 可以 ...

  4. auto property synthesis will not synthesize proterty ;it will be implementedby its superclass, use @

    Auto property synthesis will not synthesize property 'title'; it will be implemented by its supercla ...

  5. Javascript 设计模式 辛格尔顿

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/30490955 我一直很喜欢Js,,,今天写JsSingleton模式来实现,以及 ...

  6. JDBC连接数据库和释放连接

    用久了hibernate现在对于JDBC是怎么实现数据库的连接和释放,所以特地总结下关于JDBC的知识,目的是用于提醒自己很多Java的基础知识需要健全. package com.ssh.action ...

  7. Swift难点-继承中的构造规则实例具体解释

    关于继承中的构造规则是一个难点. 假设有问题,请留言问我. 我的Swift新手教程专栏 http://blog.csdn.net/column/details/swfitexperience.html ...

  8. 协同编辑多人word一个小技巧文件

    协同编辑多人word窍门 近期在工作中编写标书时因为不同内容分给了各个部门去制作.可是在汇总后遇到再次改动的问题.对方把改动后的部分文档发给我粘贴到标书中后,所有的格式所有都乱了.又一次整理格式.标题 ...

  9. CentOS下tmux安装与使用

    tmux介绍: tmux它是BSDScreen替代品,相对于Screen,它更加先进:支持屏幕切分,并且具备丰富的命令行參数,使其能够灵活.动态的进行各种布局和操作.它能够做到一条命令就启动起来(强大 ...

  10. TCP/IP详细说明--滑模、拥塞窗口、慢启动、Negle算法

    TCP的数据流大致能够分为两类,交互数据流与成块的数据流. 交互数据流就是发送控制命令的数据流.比方relogin,telnet.ftp命令等等.成块数据流是用来发送数据的包,网络上大部分的TCP包都 ...