题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表。

分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表。

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1, s2;
while(l1){
s1.push(l1 -> val);
l1 = l1 -> next;
}
while(l2){
s2.push(l2 -> val);
l2 = l2 -> next;
}
ListNode *cur = new ListNode(0);
while(!s1.empty() || !s2.empty()){
if(!s1.empty()){
cur -> val += s1.top();
s1.pop();
}
if(!s2.empty()){
cur -> val += s2.top();
s2.pop();
}
ListNode* pre = new ListNode(cur -> val / 10);
cur -> val %= 10;
pre -> next = cur;
cur = pre;
}
return (cur -> val == 0) ? cur -> next : cur;
}
};

  

												

LeetCode 445. Add Two Numbers II(链表求和)的更多相关文章

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

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

  3. 445. Add Two Numbers II 链表中的数字求和

    [抄题]: You are given two non-empty linked lists representing two non-negative integers. The most sign ...

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

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

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

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

  6. 445. Add Two Numbers II - LeetCode

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

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

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

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

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

  9. 445. Add Two Numbers II【Medium】【两个链表求和】

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

随机推荐

  1. jmeter 进行websocket数据帧过大导致code 1009

    Response message: Received: Close frame with status code 1009 and close reason 'No async message sup ...

  2. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

  3. H5拖动事件复习

    定义和用法 ondrag 事件在元素或者选取的文本被拖动时触发. 拖放是 HTML5 中非常常见的功能. 更多信息可以查看我们 HTML 教程中的 HTML5 拖放. 注意: 为了让元素可拖动,需要使 ...

  4. Win10安装.Net Framework4.7及更高版本

    问题描述 使用VS打开项目工程时,提示未安装.net framework4.7,但在启用或关闭windows功能里已经勾选了.net framework 4.7的全部功能. 直接从网上下载.net f ...

  5. SPRING MICROSERVICES IN ACTION

    What is microservice 背景 在微服务的概念成型之前,绝大部分基于WEB的应用都是使用单体的风格来进行构建的.在单体架构中,应用程序作为单个可部署的软件制品交付,所有的UI(用户接口 ...

  6. Codeforces Round #622 (Div. 2) A. Fast Food Restaurant

    Tired of boring office work, Denis decided to open a fast food restaurant. On the first day he made ...

  7. codeforces Beautiful Numbers

    来源:http://codeforces.com/problemset/problem/1265/B   B. Beautiful Numbers   You are given a permutat ...

  8. 迷のbug

    已解决(ps over%100写错了,应该是over/100...) #include <bits/stdc++.h> #define rep(i, a, b) for(int i = a ...

  9. DL4J之CNN对今日头条文本分类

    一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...

  10. A Kill Cord for your Laptop

    前言 昨晚在朋友圈看到国外一篇文章利用U盘锁笔记本电脑,刚好有一个坏的金士顿U盘,所以就折腾了一下. 准备 USB设备*1 Arch系统*1 走过的坑 因为systemd-udevd带起来的进程是ro ...