445. 两数相加 II

445. Add Two Numbers II

题目描述

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

LeetCode445. Add Two Numbers II中等

示例:

输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7

Java 实现

public class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
} @Override
public String toString() {
return val + "->" + next;
}
}
import java.util.Stack;

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>(); while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
} int sum = 0;
ListNode list = new ListNode(0);
while (!s1.empty() || !s2.empty()) {
if (!s1.empty()) sum += s1.pop();
if (!s2.empty()) sum += s2.pop();
list.val = sum % 10;
ListNode head = new ListNode(sum / 10);
head.next = list;
list = head;
sum /= 10;
}
return list.val == 0 ? list.next : list;
}
}

测试代码

import java.util.Stack;

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>(); while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
} int sum = 0;
ListNode list = new ListNode(0);
while (!s1.empty() || !s2.empty()) {
if (!s1.empty()) sum += s1.pop();
if (!s2.empty()) sum += s2.pop();
list.val = sum % 10;
ListNode head = new ListNode(sum / 10);
head.next = list;
list = head;
sum /= 10;
System.out.println(list);
}
return list.val == 0 ? list.next : list;
} public static void main(String[] args) {
ListNode t1 = new ListNode(7);
ListNode t2 = new ListNode(2);
ListNode t3 = new ListNode(4);
ListNode t4 = new ListNode(3);
t1.next = t2;
t2.next = t3;
t3.next = t4; ListNode p1 = new ListNode(5);
ListNode p2 = new ListNode(6);
ListNode p3 = new ListNode(4);
p1.next = p2;
p2.next = p3; System.out.println(new Solution().addTwoNumbers(t1, p1));
}
}

测试结果

0->7->null
1->0->7->null
0->8->0->7->null
0->7->8->0->7->null
7->8->0->7->null

相似题目

参考资料

LeetCode 445. 两数相加 II(Add Two Numbers II)的更多相关文章

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

    题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...

  2. Java实现 LeetCode 445 两数相加 II

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

  3. LeetCode 445——两数相加 II

    1. 题目 2. 解答 2.1 方法一 在 LeetCode 206--反转链表 和 LeetCode 2--两数相加 的基础上,先对两个链表进行反转,然后求出和后再进行反转即可. /** * Def ...

  4. Leetcode 445. 两数相加 II

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

  5. Java解法-两数相加(Add Two Numbers)

    问题  给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. ...

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

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  7. Leetcode 002. 两数相加

    1.题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

  8. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

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

  9. 力扣 - 445. 两数相加 II

    目录 题目 思路 代码实现 题目 给你两个 非空 链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储一位数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两 ...

随机推荐

  1. C++ vector,list,deque区别(转)

      在写C++程序的时候会发现STL是一个不错的东西,减少了代码量,使代码的复用率大大提高,减轻了程序猿的负担.还有一个就是容器,你会发现要是自己写一个链表.队列,或者是数组的时候,既要花时间还要操心 ...

  2. django-自定义文件上传存储类

    文件储存API:https://yiyibooks.cn/xx/django_182/ref/files/storage.html 编写自定义存储系统:https://yiyibooks.cn/xx/ ...

  3. How to Close Frozen Applications in macOS

    How to Close Frozen Applications in macOS By Zeeshan Akram  - February 18, 2019 0 436     Oftenly, y ...

  4. 洛谷 UVA12101 Prime Path 题解

    一道经典的BFS 用四个for搜索四位就行了,只要能推出怎么只变4位中的一位就很水了 #include<iostream> #include<cstring> #include ...

  5. NTSTATUS代码摘录

    00000000 STATUS_SUCCESS00000000 STATUS_WAIT_000000001 STATUS_WAIT_100000002 STATUS_WAIT_200000003 ST ...

  6. GitLab : Omnibus Installer

    转自:https://www.ibm.com/developerworks/community/blogs/2280dc86-a78a-441b-89d7-5b4c41595852/entry/Git ...

  7. 从一道题浅说 JavaScript 的事件循环

    最近看到这样一道有关事件循环的前端面试题: //请写出输出内容 async function async1() { console.log('async1 start'); await async2( ...

  8. GitHub页面基本知识

    官网地址:https://help.github.com/categories/github-pages-basics/ GitHub页面是什么? GitHub页面是一个静态的站点托管服务. GitH ...

  9. tensorflow学习(一)

    今天开始学习tensorflow框架,从极客学院下载了官方中文教程(15年翻译的),第一天开始学习第一章ng基本流程和原理,作为前奏.然后写了代码,验证一下,准确率确实非常高,非常好用.把代码上传,作 ...

  10. mysql pi() 获取pi

    mysql> select pi(); +----------+ | pi() | +----------+ | 3.141593 | +----------+ row in set (0.00 ...