题目链接: https://oj.leetcode.com/problems/add-two-numbers/

问题:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contains 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

解题思路:

1)用变量carry记录是否进位

2)从链表头开始计算两链表相应位置上的数字之和

注意

1)输入两链表长度不相等

2)两链表相加之后的结果链表长度可能大于两链表(例如:(9->9) + (1) = (0->0->1)

 1         public ListNode addTwoNumbers(ListNode l1, ListNode l2){
 2         ListNode dummyHead = new ListNode(0);
 3         ListNode p = l1;
 4         ListNode q = l2;
 5         ListNode curr = dummyHead;
 6         int carry = 0;
 7         while(p != null || q != null){
 8             int x = (p != null) ? p.val : 0;
 9             int y = (q != null) ? q.val : 0;
10             int digit = carry + x + y;
11             curr.next = new ListNode(digit % 10);
12             curr = curr.next;
13             carry = digit / 10;
14             if(p != null){
15                 p = p.next;
16             }
17             if(q != null){
18                 q = q.next;
19             }
20         }
21         if(carry > 0){
22             curr.next = new ListNode(carry);
23         }
24         return dummyHead.next;
25     }

转载于:https://www.cnblogs.com/momo-fun/p/5784845.html

[LeetCode] Add Two Numbers相关推荐

  1. LeetCode Add Two Numbers II(栈)

    问题:给出两个链表表示的整数,求其和 思路:因为链表的第一个结点是最高位,最后一个结点是最低位.先将两个链表放入两个栈中.然后从两个栈中取出元素,从低到高位相加. 具体代码参考: https://gi ...

  2. 每日一则 LeetCode: Add Two Numbers

    描述 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  3. LeetCode:Add Two Numbers

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

  4. leetcode:Add Two Numbers(java)

    package Leetcode;/*** 题目:* You are given two non-empty linked lists representing two non-negative in ...

  5. [LeetCode] Add Two Numbers(stored in List)

    首先,演示一个错误的reverList 1 class Solution { 2 public: 3 ListNode* reverse(ListNode* root) 4 { 5 if(NULL = ...

  6. LeetCode Add Two Numbers

    题意:链表加法 代码如下: class Solution {ListNode addTwoNumbers(ListNode l1, ListNode l2){ListNode cur1 = l1, c ...

  7. leetcode add Two Numbers

    部分 conditional operators  ?:写的statements 在有的编译器下能通过,有的可能通不过 base operand of '->' has non-pointer ...

  8. LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

    题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...

  9. [Leetcode] 445. Add Two Numbers II

    问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...

最新文章

  1. c++ 怎么读取rtf文件_最全集合!Word、PPT、Excel、PDF文件转换方法大全!建议收藏...
  2. 脚本必须位于html的,js 前端第三剑客
  3. activity7 拖不动_Activiti7相关问题汇总
  4. HUAWEI nova 青春版闪速快充,让追剧不再断电
  5. 多线程 调用 axis 报错_java笔记录(三、多线程)
  6. SpringMVC中@Controller和@RequestMapping
  7. Android学习笔记---27_网络通信之通过GET和POST方式提交参数给web应用,以及使用httpClient,来给web项目以post方式发送参数
  8. ❤️《Mybatis从基础到高级》(建议收藏)❤️
  9. C++ STL string字符串替换 replace函数的使用
  10. 自制QQ机器人插件笔记[nonebot2部署于ubuntu系统服务器]
  11. 向日葵远程控制软件——使用方法(含MacOS)
  12. Roaring 20s(还有一些《A ConvNet for the 2020s》读后感)
  13. No ulink device found肿么办
  14. PHP preg_match()函数
  15. 化工机械基础试题及答案
  16. Allegro174版本新功能介绍之和172版本兼容设置
  17. php通信软件培训,小蚂蚁学习APP接口开发(3)—— 统一调用入口方式封装通信接口...
  18. 基于国产FPGA 的MIPI硬核应用
  19. 使用Docker 镜像
  20. Java 遍历 Map 的几种方式

热门文章

  1. jinja filter
  2. opencv BRIEF角检测
  3. opencv 数学操作
  4. c ++ helloworld
  5. spark java foreach_Spark Java使用DataFrame的foreach/foreachPartition
  6. 内卷了!DAS、NAS、SAN区别和FC SAN存储
  7. VMware vCenter Server Appliance Photon OS安全修补程序
  8. python模拟seo_Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)_天津SEO...
  9. mycat分布式mysql中间件(自增主键)
  10. C#开发命令执行驱动程序 之 控制标志的命令行参数