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

分析:计算两个链表的和

解法:这道题的关键点在于节点的计算方式:sum = node1 + node2; new_node = sum%10; sum = sum/10;

public class ListNode {
  int val;
  ListNode next;
  ListNode(int x) { val = x; }
}

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  int carry =0;

  ListNode newHead = new ListNode(0);
  ListNode p1 = l1, p2 = l2, p3=newHead;

  while(p1 != null || p2 != null){
    if(p1 != null){
      carry += p1.val;
      p1 = p1.next;
    }

    if(p2 != null){
      carry += p2.val;
      p2 = p2.next;
    }

    p3.next = new ListNode(carry%10);
    p3 = p3.next;
    carry /= 10;
  }

  if(carry==1)
  p3.next=new ListNode(1);

  return newHead.next;
}

[LeetCode]-algorithms-Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. 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

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

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

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

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

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

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

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  10. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. dp入门题(数塔)

    http://acm.hdu.edu.cn/showproblem.php?pid=2084 题意: 7 3  8 8 1   0 2      7 4   4 4 5 2    6     5 在上 ...

  2. [BZOJ 1013] [JSOI2008]球形空间产生器

    [BZOJ 1013] [JSOI2008]球形空间产生器 题面 给出一个n维球体上的n+1个点,求球心坐标 分析 设球心坐标为\((x_1,x_2,\dots x_n)\),由于一个球体上的所有点到 ...

  3. numpy-添加操作大全

    合并 hstack(tup):按行合并 [前面有个 h,可以理解为 行,这样方便记忆] vstack(tup):按列合并 参数虽然是 tuple,但是 list 也行,可以合并2个或者多个数组. a= ...

  4. java 实现傅立叶变换算法 及复数的运算

    最近项目需求,需要把python中的算法移植到java上,其中有一部分需要用到复数的运算和傅立叶变换算法,废话不多说 如下: package qrs; /** * 复数的运算 * */ public ...

  5. JVM内存结构思维导图

  6. 使用Servlet实现验证码

    没有验证码带来的问题 对特定用户不断登录破解密码. 对某个网站创建账户. 对某个网站提交垃圾数据. 对某个网站刷票.  通过验证码由用户肉眼识别其中的验证码信息,从而区分用户是人还是计算机. 定义: ...

  7. day04-jQuery

    jQ宗旨:write less do more jq是js的框架,底层封装了js代码. jq引入: <script type="text/javascript" src=&q ...

  8. uni-app导航栏配置

    uni-app写app的内容会与沉浸栏重合在一起,写好好多,都是有点问题的,这次终于找到解决的方法了,与大家分享一下 最简单的解决方式就是配置mainfest.json来关闭沉浸式.即通过打开应用的m ...

  9. WPF C# 字符串读写文件

    WPF C# 字符串读写文件 public class 字符串读写文件 { /// <summary> /// Encoding.Unicode.GetString 如果使用Encodin ...

  10. INSERT - 在表中创建新行

    SYNOPSIS INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT ...