LeetCode 2. Add Two Numbers

原创 2016年08月29日 19:00:38

题目:

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

题意:

给定2个非负数链表,求出2链表相加后的新链表。

题解:(By python)

在求2条链表(如A+B)之和,需考虑2条链表是否相等及不等的情况。

# 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
        """
        res=[(l1.val+l2.val)%10]
        kes=[(l1.val+l2.val)/10]
        l1=l1.next
        l2=l2.next
        k=1
        while(l1 != None and l2 !=None):    #l1 与 l2 均未遍历完
            res.append((l1.val+l2.val+kes[k-1])%10)
            kes.append((l1.val+l2.val+kes[k-1])/10)
            k+=1
            l1=l1.next
            l2=l2.next
        while(l1 !=None and l2 == None):    #l1 比 l2长时
            res.append((l1.val+kes[k-1])%10)
            kes.append((l1.val+kes[k-1])/10)
            k+=1
            l1=l1.next
        while(l1 ==None and l2 != None):    #l2 比 l1长时
            res.append((l2.val+kes[k-1])%10)
            kes.append((l2.val+kes[k-1])/10)
            k+=1
            l2=l2.next
        if kes[k-1]>0:               #链表最后一位处理
            res.append(kes[k-1]%10)
        return res


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode: Add Two Numbers (2)

You are given two linked lists representing two non-negative numbers. The digits are stored in orde...
  • lzmaths
  • lzmaths
  • 2013年12月18日 14:19
  • 375

LeetCode Algorithms 2. Add Two Numbers 题解

题目: You are given two non-empty linked lists representing two non-negative integers. The digits...

LeetCode#2 Add Two Numbers

题目:You are given two non-empty linked lists representing two non-negative integers. The digits are s...

leetcode 2. Add Two Numbers

1.题目描述                                                                                              ...

算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers

题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits...

leetCode 2 Add Two Numbers

问题:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits...

Leetcode 2. Add Two Numbers

leetcode页面样式换了,标题和total accepted也截不到了。就直接粘描述吧 You are given two linked lists representing two non-n...

leetcode #2 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

LeetCode之2— Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored...

Leetcode #2 Add Two Numbers 解题小结

1 题外话我又回来更新Leetcode了,现在准备有空做做leetcode,等着用java刷完以后再切换到其他语言试试吧~~。 2 题目理解[原题](https://leetcode.com/prob...
  • MebiuW
  • MebiuW
  • 2016年03月05日 15:40
  • 656
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 2. Add Two Numbers
举报原因:
原因补充:

(最多只允许输入30个字)