zoukankan      html  css  js  c++  java
  • leetcode-mid-Linked list-2 Add Two Numbers

    mycode 87.22%

    # 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
            """
            dummy = link = ListNode(-1)
            add = 0
            while l1 and l2:
                temp = l1.val + l2.val + add
                if temp >= 10:
                    link.next = ListNode(temp%10)
                    add = temp // 10
                else:
                    add = 0
                    link.next = ListNode(temp)
                print(temp)
                l1 = l1.next
                l2 = l2.next
                link = link.next
            l1 = l1 or l2
            while l1:
                temp = l1.val + add
                if temp >= 10:
                    link.next = ListNode(temp%10)
                    add = temp // 10
                else:
                    add = 0
                    link.next = ListNode(temp)
                print(temp)
                l1 = l1.next
                link = link.next
            if add:
                link.next = ListNode(add)
                link = link.next
            link.next = None
            return dummy.next
                
            

    参考:

    1、如何把其中一个为None放到while里面去?

    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            dummy = link = ListNode(-1)
            add = 0
            while l1 or l2:
                res = 0
                if not l2: l2 = ListNode(0)
                if not l1:
                    l1 = ListNode(0)
                temp = l1.val + l2.val + add
                print('temp...',temp+add)
                if temp > 9:
                    res = temp % 10 
                    add = temp // 10  
                    #print('if...',res,add)
                else:
                    res = temp
                    add = 0
                    #print('else...',res,add)
                l1 = l1.next
                l2 = l2.next
                dummy.next = ListNode(res)
                dummy = dummy.next
            if add > 0:
                print('addd...')
                dummy.next = ListNode(add)
                dummy = dummy.next
                dummy.next = None
            return link.next

    2、如何把进位也放进去?

    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            dummy = cur = ListNode(0)
            curry = 0
            while l1 or l2 or curry:
                if l1:
                    curry = curry+l1.val
                    l1 = l1.next
                if l2:
                    curry = curry+l2.val
                    l2 = l2.next
                cur.next = ListNode(curry%10)
                cur = cur.next
                curry = curry//10       
            return dummy.next

    5Longest Palindromic Substring

  • 相关阅读:
    【转】2008年中国.NET技术应用趋势分析
    VB计算器(给上学需要应付作业的应个急)
    创业失败的18个原因
    使用ChilkatDotNet组件构建网络爬虫程序
    使用文本编辑器开发和部署一个ASP.NET Web应用程序
    优化 SQL Server 查询性能
    【转】sql性能优化方法
    使用 SOS 对 Linux 中运行的 .NET Core 进行问题诊断
    MultiThread Of Member Functions
    汇编 中断调用表 (中断向量表)
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10964440.html
Copyright © 2011-2022 走看看