关闭

2_Add Two Numbers

标签: leetcode
216人阅读 评论(0) 收藏 举报
分类:

You are given two non-empty linked lists representing two non-negative integers. 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.
给你两个非空链表,代表两个整形数值,数值中的数字被反序装在链表中,链表的每个结点装了一个数字,本题要你返回这两个数值相加后的结果,结果也反序装在链表中

You may assume the two numbers do not contain any leading zero, except the number 0 itself.
你可以假设这两个数值不是以0开头,也就是假设这两个数值是正常合法的

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

我的解:

# 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
        """
        p = 0
        result = None
        current = None
        while True:
            addValue = l1.val + l2.val + p
            p = 0

            if addValue >= 10:
                addValue = addValue - 10
                p = 1

            node = ListNode(addValue)

            if result == None:
                result = node
                current = node
            else:
                current.next = node
                current = node

            l1 = l1.next
            l2 = l2.next

            if l1 == None or l2 == None:
                break

        if l1 == None and l2 == None:
            pass
        elif l1 == None or l2 == None:
            rest = None
            if l1 == None:
                rest = l2
            else :
                rest = l1

            while True:
                addValue = p + rest.val
                p = 0

                if addValue >= 10:
                    addValue = addValue - 10
                    p = 1

                node = ListNode(addValue)
                current.next = node
                current = node

                rest = rest.next
                if rest == None:
                    break


        if p == 0:
            return result
        else:
            node = ListNode(p)
            current.next = node
            return result

看了答案才知道可以更简洁一些的 ~_~

0
0
查看评论

【LeetCode】2_Add Two Numbers

题目 Add Two Numbers  Total Accepted: 81118 Total Submissions: 398160My Submissions Question  Solution  You are...
  • fangjin_kl
  • fangjin_kl
  • 2015-08-18 20:56
  • 187

LeetCode 2_Add Two Numbers

这是LeetCode第二题,难度和第一题一样,也是Medium(中等)。 原题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse orde...
  • cyfcsd
  • cyfcsd
  • 2015-07-02 16:14
  • 411

LeetCode之2_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 s...
  • firechungelaile
  • firechungelaile
  • 2016-04-04 17:58
  • 191

【LeetCode】2. Add Two Numbers 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/problems/add-two-numbers/ You are given two linked l...
  • crazy1235
  • crazy1235
  • 2016-10-24 19:46
  • 2733

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 s...
  • lanxu_yy
  • lanxu_yy
  • 2013-09-14 17:06
  • 15471

LeetCode 421 Maximum XOR of Two Numbers in an Array 解题报告

一道有意思的题目, 用到了异或运算的一个小特性. LeetCode 421 Maximum XOR of Two Numbers in an Array. Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai &...
  • hanzheng992
  • hanzheng992
  • 2017-02-02 11:27
  • 752

LeetCode Add Two Numbers 之JavaScript 多种解法

LeetCode Add Two Numbers 之JavaScript 多种解法
  • weixin_36751895
  • weixin_36751895
  • 2017-07-13 12:45
  • 387

Leetcode中Add Two Numbers算法的理解

一、题目 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes ...
  • zhangguijuansc
  • zhangguijuansc
  • 2017-06-04 14:55
  • 383

2、add two numbers(python)

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 ...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:49
  • 487

【python】【leetcode】【算法题目2—Add Two Numbers】

一、题目描述 题目原文:  You are given two linked lists representing two non-negative numbers.  The digits are stored in reverse order...
  • u014615155
  • u014615155
  • 2016-11-21 12:05
  • 1959
    个人资料
    • 访问:122075次
    • 积分:2649
    • 等级:
    • 排名:第16266名
    • 原创:136篇
    • 转载:56篇
    • 译文:0篇
    • 评论:36条
    博客专栏