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:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7 7->2->4->3
5->6->4
=-------------
7->7->0->7
->1
"""
s1,s2 = [],[]
p1,p2 = l1,l2
while p1:
s1.append(p1.val)
p1 = p1.next
while p2:
s2.append(p2.val)
p2 = p2.next
carry = 0
fake_head = ListNode(None)
while s1 or s2 or carry:
v1 = s1.pop() if s1 else 0
v2 = s2.pop() if s2 else 0
val = v1 + v2 + carry
if val >= 10:
val -= 10
carry = 1
else:
carry = 0
head = ListNode(val)
head.next = fake_head.next
fake_head.next = head
return fake_head.next

445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例的更多相关文章

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

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

  2. 445. Add Two Numbers II - LeetCode

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

  3. LeetCode 445 Add Two Numbers II

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

  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. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

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

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

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

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

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

  9. *445. Add Two Numbers II

    1. 原始题目 You are given two non-empty linked lists representing two non-negative integers. The most si ...

随机推荐

  1. Python基础学习笔记(六)常用列表操作函数和方法

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-lists.html 3. http://www.liaoxuef ...

  2. RedHat安装DB2详细步骤(附卸载、备份恢复步骤)

    1.创建用户组和用户 说明: 步骤1 以root用户登录需要安装DB2的服务器. 步骤2 创建用户组和用户. # su -root # groupdel db2grp # groupdel db2fg ...

  3. Log4j XML 配置

    Xml代码 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configurat ...

  4. php笔记[1]

    1:echo '$temp tires<br />'; 该代码将'$temp tires<br />'发送给浏览器,在双引号中,变量名将被变量值所替代.而在单引号中,变量名称, ...

  5. iOS - OC NSPoint 位置

    前言 结构体,这个结构体用来表示事物的一个坐标点. typedef CGPoint NSPoint; struct CGPoint { CGFloat x; CGFloat y; }; typedef ...

  6. Switch用法

    package com.cz.test; public class SwitchExample1 { /** * @param args */ public static void main(Stri ...

  7. LCM兼容

    1.project-1998-trunk-bootable-bootloader-lk-project:   复制zaw1998aa_platform.mk为zaw2000aa_platform.mk ...

  8. otl插入数据不成功

    原因是:void rlogon(...); 没有设置auto_commit为1,otl不会自动提交. 注意:static int otl_initialize (const int threaded_ ...

  9. mysql /*! 50100 ... */ 条件编译

    1./*...*/ 是注释,mysql不会执行.2.mysql对标准sql进行了扩展,包含了一些自己的特性.3./*!...*/ 是一种特殊的注释,其他的数据库产品当然不会执行.mysql特殊处理,会 ...

  10. Oracle 中union的用法

    UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. 例如: SELECT Date FROM Store_Information UNION SELECT Date ...