python3兩數(shù)相加的實(shí)現(xiàn)示例
兩數(shù)相加
給你兩個(gè) 非空 的鏈表,表示兩個(gè)非負(fù)的整數(shù)。它們每位數(shù)字都是按照 逆序 的方式存儲(chǔ)的,并且每個(gè)節(jié)點(diǎn)只能存儲(chǔ) 一位 數(shù)字。
請(qǐng)你將兩個(gè)數(shù)相加,并以相同形式返回一個(gè)表示和的鏈表。
你可以假設(shè)除了數(shù)字 0 之外,這兩個(gè)數(shù)都不會(huì)以 0 開(kāi)頭。
示例 1:
輸入:l1 = [2,4,3], l2 = [5,6,4]
輸出:[7,0,8]
解釋:342 + 465 = 807.
示例 2:
輸入:l1 = [0], l2 = [0]
輸出:[0]
示例 3:
輸入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
輸出:[8,9,9,9,0,0,0,1]
思路:
1.創(chuàng)建一個(gè)新鏈表,新鏈表的頭部先設(shè)置為l1頭部和l2頭部之和。
2.遍歷兩個(gè)鏈表,只要有一個(gè)還沒(méi)有遍歷完就繼續(xù)遍歷
3.每次遍歷生成一個(gè)當(dāng)前節(jié)點(diǎn)cur的下一個(gè)節(jié)點(diǎn),其值為兩鏈表對(duì)應(yīng)節(jié)點(diǎn)的和再加上當(dāng)前節(jié)點(diǎn)cur產(chǎn)生的進(jìn)位
4.更新進(jìn)位后的當(dāng)前節(jié)點(diǎn)cur的值
5.循環(huán)結(jié)束后,因?yàn)槭孜豢赡墚a(chǎn)生進(jìn)位,因此如果cur.val是兩位數(shù)的話,新增一個(gè)節(jié)點(diǎn)
6.返回頭節(jié)點(diǎn)
由題目注釋可以看出listNode這個(gè)類是用來(lái)創(chuàng)建鏈表的,默認(rèn)next=None,val=0.
Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: head = ListNode(l1.val+l2.val) current = head while l1.next or l2.next: l1 = l1.next if l1.next else ListNode() l2 = l2.next if l2.next!=None else ListNode() current.next = ListNode(l1.val+l2.val+current.val//10) current.val = current.val%10 current = current.next if current.val >= 10: current.next = ListNode(current.val//10) current.val = current.val%10 return head
改進(jìn)改進(jìn):增加了空間復(fù)雜度。本以為一方為None后直接把另一個(gè)鏈表連上就ok了。然后,就打臉了。
然后又加了while
> [9999] # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: head = ListNode(l1.val+l2.val) current = head while l1.next and l2.next: l1 = l1.next l2 = l2.next current.next = ListNode(l1.val+l2.val+current.val//10) current.val = current.val%10 current = current.next if l1.next == None and l2.next : while l2.next: l2 = l2.next current.next= ListNode(l2.val+current.val//10) current.val = current.val%10 current = current.next current.next = l2.next elif l2.next == None and l1.next: while l1.next: l1 = l1.next current.next= ListNode(l1.val+current.val//10) current.val = current.val%10 current = current.next current.next = l2.next if current.val >= 10: current.next = ListNode(current.val//10) current.val = current.val%10 return head
到此這篇關(guān)于python3兩數(shù)相加的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)python3兩數(shù)相加內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django contenttypes 框架詳解(小結(jié))
這篇文章主要介紹了Django contenttypes 框架詳解(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08利用Python判斷文件的幾種方法及其優(yōu)劣對(duì)比
任何的編程語(yǔ)言都離不開(kāi)文件操作,文件操作主要是幫助我們更好的創(chuàng)建打開(kāi)文件,讀取文件中的內(nèi)容、或是把我們想要保存的內(nèi)容寫入文件中、或是管理文件目錄等等,下面這篇文章主要給大家介紹了關(guān)于利用Python判斷文件的幾種方法及其優(yōu)劣對(duì)比的相關(guān)資料,需要的朋友可以參考下2022-06-06探索Python內(nèi)置數(shù)據(jù)類型的精髓與應(yīng)用
本文探索Python內(nèi)置數(shù)據(jù)類型的精髓與應(yīng)用,包括字符串、列表、元組、字典和集合。通過(guò)深入了解它們的特性、操作和常見(jiàn)用法,讀者將能夠更好地利用這些數(shù)據(jù)類型解決實(shí)際問(wèn)題。2023-09-09總結(jié)python多進(jìn)程multiprocessing的相關(guān)知識(shí)
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著python multiprocessing多進(jìn)程的相關(guān)知識(shí)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06