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

分析:用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. Python学习之面向对象进阶

    面向对象进阶当然是要谈谈面向对象的三大特性:封装.继承.多态 @property装饰器 python虽然不建议把属性和方法都设为私有的,但是完全暴露给外界也不好,这样,我们给属性赋值的有效性九无法保证 ...

  2. thinkphp 3.2链接Oracle数据库,查询数据

    ennnn,换工作了,开始用新的东西了,最近就是调用nc接口,数据库是Oracle,首先先把数据查出来,这个比较简单. 在网上看的其他的方法都是改数据库配置文件,然后需要修改tp核心的一个类文件,比较 ...

  3. 用xshell连接VMware虚拟机中安装的Centos7系统

    首先要保证你安装的Centos7系统的网路适配器使用的桥接模式,这个模式允许你安装再虚拟机中的Centos系统有一个自己的ip地址. 然后再虚拟机中登录你的Centos系统,用ip addr命令查看你 ...

  4. EVE上传Dynamips、IOL和QEMU镜像

    1.镜像保存目录: /opt/unetlab/addons ---/dynamips   Dynamips镜像保存目录 ---/iol               IOL镜像保存目录(运行IOU的镜像 ...

  5. mysql取出字段数据的精度

    $field = 'convert(avg(mood),decimal(4,0)) mood,convert(avg(hrv),decimal(4,0)) hrv,convert(avg(heart_ ...

  6. @Value注解的使用

    前提它需要在spring 管理的Bean中有效 (如@Service...) #{...} 此方式可以使用 SpEL 表达式如 #{30-15} ${...} 可以获取配置文件中的值 如 ${jwt. ...

  7. Java基础题目

    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 程序分析:兔子的规律为数列1,1,2,3,5,8, ...

  8. Python的基础知识,不同于其他编程语言

    1.字符串拼接可以不使用+号 name = "this " "is " "a " "string" 2.使用''' ‘’ ...

  9. 说明与比较:new Vue() 和 export default {}

    在生成.导出.导入.使用 Vue 组件的时候,像我这种新手就会常常被位于不同文件的 new Vue() 和 export default{}.它们含义到底是什么,又有什么异同呢? 首先,Vue 是什么 ...

  10. Maven与Nexus

    开始在使用Maven时,总是会听到nexus这个词,一会儿maven,一会儿nexus,当时很是困惑,nexus是什么呢,为什么它总是和maven一起被提到呢? 我们一步一步来了解吧. 一.了解Mav ...