445. Add Two Numbers II【Medium】【两个链表求和】
You are given two non-empty linked lists representing two non-negative integers. 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:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>(); while (l1 != null) {
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
stack2.push(l2.val);
l2 = l2.next;
}
//node的下一个结点为head
ListNode head = new ListNode(0); //head记录结果
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (!stack1.isEmpty()) head.val += stack1.pop();
if (!stack2.isEmpty()) head.val += stack2.pop();
ListNode node = new ListNode(head.val / 10); //node记录进位
head.val %= 10; //head存储结果
node.next = head; //head往前移动,指向node
head = node;
}
//前导0的情况,head为node的引用,可能为0
return head.val == 0 ? head.next : head;
}
}

445. Add Two Numbers II【Medium】【两个链表求和】的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- *445. Add Two Numbers II
1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...
- 【Leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
随机推荐
- windows版本redis下载安装
官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 在官方下载网页中有一个window版本的说明,说redis官方没有wi ...
- Web Service快速入门
一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 那么它是如何做到这种跨语言,跨平台之间的调用呢? 其实它是以一个xml文件以及webservice这种服务来实现跨平台 ...
- 数据结构&图论:欧拉游览树
ETT可以称为欧拉游览树,它是一种和欧拉序有关的动态树(LCT是解决动态树问题的一种方案,这是另一种) dfs序和欧拉序是把树问题转化到区间问题上然后再用数据结构去维护的利器 通过借助这两种形式能够完 ...
- 【BZOJ4903】【CTSC2017】吉夫特 [DP]
吉夫特 Time Limit: 15 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行一个整数n. 接下 ...
- 面试精选之Promise
常见Promise面试题 我们看一些 Promise 的常见面试问法,由浅至深. 1.了解 Promise 吗? 2.Promise 解决的痛点是什么? 3.Promise 解决的痛点还有其他方法可以 ...
- Tornado 安装及简单程序示例
1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...
- esp8266 IOT Demo 固件刷写记录
将编译好的固件按照下面地址刷写到esp8266 出现下面错误是因为刷写的设置不对,按照图上设置: load 0x40100000, len 26828, room 16 tail 12chksum 0 ...
- Python3 嵌套函数
嵌套函数: 函数体内用def定义函数 注意:函数体中调用其他函数不算嵌套函数,只能是函数的调用 简单的嵌套函数: 输出结果:
- 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week1 Introduction to deep learning课堂笔记
Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week1 Introduction to deep learn ...
- 用tkinter实现的gui小工具
import tkinter import requests import json from tkinter import * class FindLocation(object): def __i ...