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)文章
Python析構(gòu)函數(shù)__del__定義原理解析
這篇文章主要介紹了Python析構(gòu)函數(shù)__del__定義原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Python刪除Java源文件中全部注釋的實(shí)現(xiàn)方法
這篇文章主要介紹了Python刪除Java源文件中全部注釋的實(shí)現(xiàn)方法,涉及Python讀取文件、正則匹配、字符串查找、替換等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python進(jìn)階教程之創(chuàng)建本地PyPI倉(cāng)庫(kù)
pypi是一個(gè)python包的倉(cāng)庫(kù),里面有很多別人寫(xiě)好的python庫(kù),你可以通過(guò)easy_install或者pip進(jìn)行安裝,下面這篇文章主要給大家介紹了關(guān)于Python進(jìn)階教程之創(chuàng)建本地PyPI倉(cāng)庫(kù)的相關(guān)資料,需要的朋友可以參考下2021-10-10
Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程
Python實(shí)現(xiàn)PDF與多種圖片格式之間互轉(zhuǎn)(PNG, JPG, BMP, EMF,&nb
Django Auth應(yīng)用實(shí)現(xiàn)用戶身份認(rèn)證
用Python解決計(jì)數(shù)原理問(wèn)題的方法
淺談django2.0 ForeignKey參數(shù)的變化

