[LeetCode][Python]Add Two Numbers

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘https://oj.leetcode.com/problems/add-two-numbers/

You are given two linked lists representing two non-negative numbers.The digits are stored in reverse order and each of their nodes contain a single digit.Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8

===Comments by Dabay===这道题没有要求不使用额外的空间。思路比较简单,就是逐个加起来的同时考虑进位。那就在已有的空间上操作,主要是处理一些细节问题,如末尾有进位、两个数组不一样长等等。我这里为了省事,当l1不够l2长的时候,用了额外的空间。‘‘‘

# Definition for singly-linked list.class ListNode:    def __init__(self, x):        self.val = x        self.next = None

class Solution:    # @return a ListNode    def addTwoNumbers(self, l1, l2):        if l1 is None:            return l2        if l2 is None:            return l1

        node1, node2, carry, end = l1, l2, 0, None        while node1 or node2:            if not node1:                node1 = ListNode(0)                end.next = node1            if not node2:                node2 = ListNode(0)            v = node1.val + node2.val + carry            carry = 0            if v >= 10:                v = v - 10                carry = 1            node1.val = v            end = node1            node1 = node1.next            node2 = node2.next        if carry:            end.next = ListNode(1)        return l1

def main():    root1 = ListNode(2)    root1.next = ListNode(4)    root1.next.next = ListNode(3)    root2 = ListNode(5)    root2.next = ListNode(6)    root2.next.next = ListNode(4)    s = Solution()    root = s.addTwoNumbers(root1, root2)    node = root    while node:        print "%s->" % node.val,        node = node.next    print "None"

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-10-14 01:33:18

[LeetCode][Python]Add Two Numbers的相关文章

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

leetcode -day20 Add Two Numbers

1.  Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 ->

LeetCode OJ - Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

[Leetcode]Add Two Numbers

题目 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&g

[C++]LeetCode: 108 Add Two Numbers (反序链表求和)

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

[LeetCode][JavaScript]Add Two Numbers

Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (