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


题解:模拟即可。用carries保存进位,当l1和l2一方为空的时候,余下不为空的链表要单独处理。当l1和l2都为空的时候,如果carries不为空,那么要再单独申请一个链表存放carries,代码如下:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  14. if(l1 == null && l2 == null)
  15. return null;
  16.  
  17. ListNode kepeler = new ListNode(0);
  18. ListNode head = kepeler;
  19. int carries = 0;
  20.  
  21. while(l1 != null && l2 != null){
  22. int sum = carries + l1.val + l2.val;
  23. carries = sum/10;
  24. kepeler.next = new ListNode(sum%10);
  25. kepeler = kepeler.next;
  26. l1 = l1.next;
  27. l2 = l2.next;
  28. }
  29.  
  30. while(l1 != null){
  31. int sum = carries + l1.val;
  32. carries = sum/10;
  33. kepeler.next = new ListNode(sum%10);
  34. l1 = l1.next;
  35. kepeler = kepeler.next;
  36. }
  37.  
  38. while(l2 != null){
  39. int sum = carries + l2.val;
  40. carries = sum/10;
  41. kepeler.next = new ListNode(sum%10);
  42. l2 = l2.next;
  43. kepeler = kepeler.next;
  44. }
  45.  
  46. if(carries != 0)
  47. kepeler.next = new ListNode(carries);
  48.  
  49. return head.next;
  50. }
  51. }

【leetcode刷题笔记】Add Two Numbers的更多相关文章

  1. LeetCode刷题系列——Add Two Numbers

    题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...

  2. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  3. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  4. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. 【leetcode刷题笔记】Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

随机推荐

  1. 并发检测主机ip存活脚本

    #!/bin/bash ################### # Check Active Host####### ################### function CheckInput() ...

  2. 读《《图解TCP-IP》》有感

    读<<图解TCP/IP>>有感 TCP/IP 近期几天读完<<图解TCP/IP>>,收获蛮多,记得上学时读stevens的<<TCP/IP具 ...

  3. task19-21

    [说明]理想是丰满的,现实很骨感,昨天还说今天有望干掉5个小任务,看来是没可能了,兜兜转转地做了一天也才完成下面的这些 一:今日完成 19.学习Spring,配置Spring和Junit 1)先安装一 ...

  4. Js遍历对象中的属性

    var obj = {"name": "xiaol","age":"18"} for(var attr in obj){ ...

  5. Linux c编程:线程属性

    前面介绍了pthread_create函数,并且当时的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结构的指针.可以使用pthread_attr_t结构修改线程默认属性,并把这些 ...

  6. Visual Studio 2017 扩展推荐

    ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G,就不要使用它了.它的特点可以快速重构.高亮显示错误.导航和 ...

  7. UML建模:学习笔记(1)

    UML:学习笔记(1) 事物 结构事物 类: 接口: 协作:(定义元素之间的相互作用) 用例:(在系统外部和系统交互的人) 组件:(描述物理系统的一部分) 节点:(一个节点可以被定义为运行时存在的物理 ...

  8. HTML 获取屏幕,浏览器,页面的高度

    1,物理尺寸和分辨率 容器的尺寸是指当前分辨率下的高度.宽度,而不是物理高度.宽度. 如:一个22寸的显示器,屏幕分辨率为1366 * 768,那么获取到的屏幕高度为1366px,宽度为768px. ...

  9. 【leetcode刷题笔记】Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  10. [算法]Rotate Array

    You may have been using Java for a while. Do you think a simple Java array question can be a challen ...