LeetCode链表相加-Python<二>

上一篇:LeetCode两数之和-Python<一>

题目:https://leetcode-cn.com/problems/add-two-numbers/description/

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

关键点:进位(这里满十加一)、指针移动(这里移向下一个节点)

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        ans = ListNode(0)  #新建一个节点,初始值为0
        temp = ans
        tempsum = 0

        while True:
            if (l1 != None):
                tempsum = l1.val + tempsum #l1链表节点值添加到总和里
                l1 = l1.next               #指针指向下一个节点
            if (l2 != None):
                tempsum = tempsum + l2.val #l2链表节点值添加到总和里
                l2 = l2.next               #指针指向下一个节点

            temp.val = tempsum % 10        #取余数(满十进位),赋值当前节点值
            print(tempsum)
            tempsum  = int(tempsum / 10)   #获取进位数赋值给总和(比如tempsum为10则进1位,否则进位为0),下一次节点相加,从新的总和开始。
            if l1 == None  and l2 == None and tempsum == 0: #直到没有进位了,同时节点位空了,跳出循环。(这里加上tempsum==0条件是因为,最后两个节
                break                                         #点和值可能大于10)                

            temp.next = ListNode(0) #新建下一个节点,存放和
            temp = temp.next        #指针指向下一个节点

        return ans

时间复杂度:O(max(l1长度,l2长度)) ,空间复杂度:O(max(l1长度,l2长度))

原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/9262577.html

时间: 2024-08-05 11:43:11

LeetCode链表相加-Python<二>的相关文章

[leetcode]Sort List @ Python

原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈.   这里涉及到一个链表常用的操作,即快慢指针的技巧.设置slow和fast指针,开始它们都

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

[leetcode]Reorder List @ Python

原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,

[leetcode]Simplify Path @ Python

原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corn

[leetcode]LRU Cache @ Python

原题地址:http://oj.leetcode.com/problems/lru-cache/ 题意:设计LRU Cache 参考文献:http://blog.csdn.net/hexinuaa/article/details/6630384 这篇博文总结的很到位.   https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LRUCache.py 代码参考的github人写的,思路非常清晰,写的也很好. Cache简介: Ca

[leetcode]Two Sum @ Python

原题地址:http://oj.leetcode.com/problems/two-sum/ 题意:找出数组numbers中的两个数,它们的和为给定的一个数target,并返回这两个数的索引,注意这里的索引不是数组下标,而是数组下标加1.比如numbers={2,7,11,17}; target=9.那么返回一个元组(1,2).这道题不需要去重,对于每一个target输入,只有一组解,索引要按照大小顺序排列. 解题思路:1,由于要找到符合题意的数组元素的下标,所以先要将原来的数组深拷贝一份,然后排

leetcode 链表题总结

按照frequency来排序,总共27题 1.2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/#/description 两个链表相加,注意就是进位问题. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited