You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

  1. Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
  2. Output: 7 -> 8 -> 0 -> 7

2. Add Two Numbers 的变形,之前的题最高位在链表末位,此题链表头部表示高位,尾部表示低位,不允许反转链表。两个数相加需要从低位开始。可以利用Stack的特点后进先出,遍历两个链表,将数字分别压入两个栈s1和s2,然后开始循环,如果栈不为空,则将栈顶数字加入sum中。

Java:

  1. public class Solution {
  2. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  3. Stack<Integer> s1 = new Stack<Integer>();
  4. Stack<Integer> s2 = new Stack<Integer>();
  5.  
  6. while(l1 != null) {
  7. s1.push(l1.val);
  8. l1 = l1.next;
  9. };
  10. while(l2 != null) {
  11. s2.push(l2.val);
  12. l2 = l2.next;
  13. }
  14.  
  15. int sum = 0;
  16. ListNode list = new ListNode(0);
  17. while (!s1.empty() || !s2.empty()) {
  18. if (!s1.empty()) sum += s1.pop();
  19. if (!s2.empty()) sum += s2.pop();
  20. list.val = sum % 10;
  21. ListNode head = new ListNode(sum / 10);
  22. head.next = list;
  23. list = head;
  24. sum /= 10;
  25. }
  26.  
  27. return list.val == 0 ? list.next : list;
  28. }
  29. }

Python:

  1. class Solution(object):
  2. def addTwoNumbers(self, l1, l2):
  3. stk1, stk2 = [], []
  4. while l1:
  5. stk1.append(l1.val)
  6. l1 = l1.next
  7. while l2:
  8. stk2.append(l2.val)
  9. l2 = l2.next
  10.  
  11. prev, head = None, None
  12. sum = 0
  13. while stk1 or stk2:
  14. sum /= 10
  15. if stk1:
  16. sum += stk1.pop()
  17. if stk2:
  18. sum += stk2.pop()
  19.  
  20. head = ListNode(sum % 10)
  21. head.next = prev
  22. prev = head
  23.  
  24. if sum >= 10:
  25. head = ListNode(sum / 10)
  26. head.next = prev
  27.  
  28. return head

C++:

  1. class Solution {
  2. public:
  3. ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  4. stack<int> stk1, stk2;
  5. while (l1) {
  6. stk1.emplace(l1->val);
  7. l1 = l1->next;
  8. }
  9. while (l2) {
  10. stk2.emplace(l2->val);
  11. l2 = l2->next;
  12. }
  13.  
  14. ListNode *prev = nullptr, *head = nullptr;
  15. int sum = 0;
  16. while (!stk1.empty() || !stk2.empty()) {
  17. sum /= 10;
  18. if (!stk1.empty()) {
  19. sum += stk1.top();
  20. stk1.pop();
  21. }
  22.  
  23. if (!stk2.empty()) {
  24. sum += stk2.top();
  25. stk2.pop();
  26. }
  27.  
  28. head = new ListNode(sum % 10);
  29. head->next = prev;
  30. prev = head;
  31. }
  32.  
  33. if (sum >= 10) {
  34. head = new ListNode(sum / 10);
  35. head->next = prev;
  36. }
  37.  
  38. return head;
  39. }
  40. };

  

相似题目:

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

[LeetCode] 67. Add Binary 二进制数相加 

[LeetCode] 445. Add Two Numbers II 两个数字相加之二

[LeetCode] 445. Add Two Numbers II 两个数字相加之二的更多相关文章

  1. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

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

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  3. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  4. LeetCode 445 Add Two Numbers II

    445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...

  5. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

  6. LeetCode 445. Add Two Numbers II(链表求和)

    题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...

  7. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  8. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  9. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

随机推荐

  1. Mac Docker安装MySQL5.7

    mkdir mysql 在~目录下创建mysql目录 docker run --restart=always --name mysql5.7 -p 3306:3306 -v ~/mysql:/var/ ...

  2. Python获取当前脚本文件夹(Script)的绝对路径

    Python获取当前脚本绝对路径 Python脚本有一个毛病,当使用相对路径时,被另一个不同目录下的py文件中导入时,会报找不到对应文件的问题.感觉是当前工作目录变成了导入py文件当前目录.如果你有配 ...

  3. CSE301 – Bio-Computation

    CSE301 – Bio-Computation Assessment 3Contribution to overall module assessment 10%Submission deadlin ...

  4. java接口的成员变量的修饰符

    前言:c++学的java都忘记了不少 interface(接口)可将其想象为一个"纯"抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不实现方法主体 接 ...

  5. C# 基础回顾: volatile 关键字

    有些人可能从来没看到过这个关键字,这也难怪,因为这个关键字并不常用.那这个关键字到底有什么用呢? 我在网上搜索这个关键字的时候,发现很多朋友都有一个错误的认识 ------ 认为这个关键字可以防止并发 ...

  6. VS2010中使用boost正则表达式库

    1.下载boost库.http://www.boost.org/ 我下载的是boost_1_51_0版本.放在D:\opensource\boost_1_51_0. 2.编译boost库.     执 ...

  7. Mongo 安装及基本操作

    一. 安装 Mongo文档: https://docs.mongodb.com/v3.6/administration/install-enterprise-linux/ Linux mongo的配置 ...

  8. jmeter接口自动化和性能学习目录

     目录黑色代表未完成的,绿色代表已完成的文章.目录的作用的为了引导和总结自己的学习,也是为了更好的分享给大家. 一.接口自动化 jmeter解决登录token获取 jmeter五种提取器 之 正则表达 ...

  9. Js 之正则验证手机号、QQ、身份证等

    // 常见的 正则表达式 校验 // QQ号.手机号.Email.是否是数字.去掉前后空格.是否存在中文.邮编.身份证.URL.日期格式.IP let myRegExp = { // 检查字符串是否为 ...

  10. Koa 操作 Mongodb 数据库

    node-mongodb-native的介绍 使用基于官方的 node-mongodb-native 驱动,封装一个更小.更快.更灵活的 DB 模块, 让我们用 nodejs 操作 Mongodb 数 ...