题目:

  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

思路:采用递归思想。设置一个carray变量用于存放进位值,将l1,l2,carray相加,十进制取余得到该位的值,进行链表下一位运算。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return addTwoNumbersHelp(l1,l2,0);
} public ListNode addTwoNumbersHelp(ListNode l1,ListNode l2,int carray){
if(l1==null && l2==null){
return carray==0?null:new ListNode(carray);
} if(l1==null && l2!=null){
l1=new ListNode(0);
} if(l1!=null && l2==null){
l2=new ListNode(0);
} int sum=l1.val+l2.val+carray;
ListNode curr=new ListNode(sum%10);
curr.next=addTwoNumbersHelp(l1.next,l2.next,sum/10); return curr;
}

【LeetCode】2.Add Two Numbers 链表数相加的更多相关文章

  1. [leetcode]2. Add Two Numbers两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  2. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  3. [LeetCode]2.Add Two Numbers 两数相加(Java)

    原题地址: add-two-numbers 题目描述: 给你两个非空的链表,表示两个非负的整数.它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字. 请你将两个数相加,并以相同形式返回 ...

  4. LeetCode 2. Add Two Numbers (两数相加)

    题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...

  5. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  6. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  7. Leetcode2.Add Two Numbers两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  8. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  9. [LeetCode]2. Add Two Numbers链表相加

    注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...

随机推荐

  1. MySQL Horizontal Scaling

    http://www.tuicool.com/articles/miuq63E http://www.guokr.com/blog/475765/ http://songwie.com/article ...

  2. 关于SetTimer间隔小于OmTimer执行时间的问题

    如果SetTimer的时间间隔为t,其响应事件OnTimer代码执行一遍的时间为T,且T>t.这样,一次未执行完毕,下一次定时到,这时候程序会如何执行? 可能的情况:1.丢弃还未执行的代码,开始 ...

  3. ubuntu 14.04 部署Django项目

    一.购买服务器 推荐 vultr的服务器,还可以_ _ _,链接:传送门 操作系统建议选 ubuntu 14.04 64位 二.购买域名 链接:传送门 三.安装相关软件 # 创建一个叫mu的用户 ro ...

  4. Spring入门第九课

    使用外部属性文件 在配置文件里面配置Bean时,有时需要在Bean的配置里面混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离. Spring提供了 ...

  5. CI框架中,扩展验证码类。

    使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用.它需要写入到数据库中,然后再进行比对. 大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力. 所以,我们还是利用se ...

  6. malloc,alloc,realloc之间的相似与区别

    三个函数的申明分别是: void* realloc(void* ptr, unsigned newsize); void* malloc(unsigned size); void* calloc(si ...

  7. opengl1

    OpenGL Programming Guide Programming Guide > Chapter 1 Chapter 1 Introduction to OpenGL Chapter O ...

  8. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)

    Participate in E-sports 11.44% 1000ms 65536K   Jessie and Justin want to participate in e-sports. E- ...

  9. 715. Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  10. [Lintcode]Word Squares(DFS|字符串)

    题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...