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. MyBatis删除多个类型不一致或不在同一个对象中参数的记录

    控制层中: // 根据店家id查找图书,已售数量要大于等于1才显示 List<SoldBook> sbList = shopService.getSoldBookByShopidAndBo ...

  2. vuex的简单理解

    初次接触vuex,谈谈我自己的理解.有待后期改进 首先要知道,Vuex 是专门为 Vue.js 设计的状态管理库.我们知道在用vue.js进行前端项目开发时,会出现很多组件相互之间调用属性.状态,小项 ...

  3. composer之predis

    安装:   composer require predis/predis   即可 predis是PHP连接Redis的操作库,由于它完全使用php编写,大量使用命名空间以及闭包等功能,只支持php5 ...

  4. ajax异步 —— javascript

    目录 ajax是什么 原生ajax jquery ajax ajax跨域 ajax是什么 作用:不必重新加载整个页面,更新部分页面内容. 大概使用过程:通过后台提供的数据接口,ajax获取数据,动态修 ...

  5. ThinkPHP验证器验证规则编码要点

    首先验证器要继承框架的think\Validate类. 1.验证规则是一个父类的rule属性,是一个数组. 2.数组的键名是验证字段标识,值是验证规则.多个验证规则要用|分隔,不能有空格,否则可能会验 ...

  6. openlayers 添加标记点击弹窗 定位图标闪烁

    环境vue3.0 ,地图为公用组件,将添加图标标记的方法放在公共地图的初始化方法里 同一时间弹窗和定位标识都只有一个,因而我把弹窗和定位标记的dom预先写好放到了页面 //矢量标注样式设置函数,设置i ...

  7. SAP发布wbservice,如果有权限管控的话,需要给这个webservice加权限

    1. PFCG床架角色 2.在角色菜单上,添加其他,选中Authorization Default Values for Services 如下图 3.选中发布的webservice 后保存,如下图: ...

  8. excle 文件的导入和导出

    //excle 文件导出 public function excel(){ try{ include(BASE_PATH."Excel/PHPExcel.php"); // ech ...

  9. UIWebView半透明设置

    在项目中有时候需要弹出活动弹框,由于原生的样式会固定,所以考虑h5显示,这就需要webView的背景色半透明,如图: 让 UIWebView 背景透明需要以下设置

webView.backgroun ...

  10. 查看mysql数据库容量大小

     第一种情况:查询所有数据库的总大小,方法如下: mysql> use information_schema; mysql> select concat(round(sum(DATA_LE ...