Leetcode 兩數相加

2021-10-03 20:49:58 字數 4091 閱讀 4718

#!/usr/bin/env python

# -*- coding:utf-8 -*-

class

listnode

(object):

def__init__

(self,val=

none):

self.val = val

self.

next

=none

class

listnode_handle

:# 定義單鏈表的各種操作

def__init__

(self)

: self.cur_node =

none

# self.nodelist=none # 用來儲存結果的鍊錶,方法3?????

defadd

(self, data)

:# 頭插法(每次新增新節點都是在首節點之後新增)

node = listnode(

) node.val = data

node.

next

= self.cur_node

self.cur_node = node # cur_node總是指向新節點

return node

defprint_listnode

(self, node)

:while node:

# 當條件變成node.val時可能報錯,最後乙個元素可能是空節點,它沒有資料域和指標域

# print ('\nnode: ', node, ' value: ', node.val, ' next: ', node.next)

print

(node.val)

node = node.

next

defget_list

(self, nodelist)

:# 能否實現利用for 迴圈快速遍歷

list=[

]while nodelist:

list

nodelist = nodelist.

next

return

list

def__next__

(self,node)

:# 奇思妙想:就是要遍歷當前node節點後所有的元素

pass

class

solution

(object):

defaddtwonumbers

(self, l1, l2)

: carry =

0 result_handle = listnode_handle(

) result = listnode(

)while l1 !=

none

or l2 !=

none:if

not carry:

# 無進製

if l1 !=

none

and l2 !=

none

: l3_val = l1.val + l2.val

elif l1 ==

none

and l2 !=

none

: l3_val = l2.val

elif l1 !=

none

and l2 ==

none

: l3_val = l1.val

else

:pass

else

:# 有進製

if l1 !=

none

and l2 !=

none

: l3_val = l1.val + l2.val +

1elif l1 ==

none

and l2 !=

none

: l3_val = l2.val +

1elif l1 !=

none

and l2 ==

none

: l3_val = l1.val +

1else

:pass

carry =

0# 處理完進製後,將進製標誌置空

if l3_val <10:

result = result_handle.add(l3_val)

else

: result = result_handle.add(l3_val %10)

carry = l3_val //

10try

: l1 = l1.

next

except

:pass

try:

l2 = l2.

next

# l2 為none,執行except語句

except

:pass

if carry:

result = result_handle.add(carry)

# result_handle.print_listnode(result)

return result

class

solution1

:# @return a listnode

defaddtwonumbers

(self, l1, l2)

: carry =

0 root = n = listnode(

'prior'

)# root 始終指向第乙個頭結點,n節點則是指向新加入的節點

while l1 or l2 or carry:

# 防止l1 or l2 為空鍊錶

v1 = v2 =

0if l1:

v1 = l1.val

l1 = l1.

next

if l2:

v2 = l2.val

l2 = l2.

next

carry, val =

divmod

(v1 + v2 + carry,

10)

n.next

= listnode(val)

# 尾插法新增新節點

n = n.

next

return root.

next

# 結果時逆序的

if __name__ ==

"__main__"

: listnode_1 = listnode_handle(

) l1 = listnode(

) l1_list =[9

,9]# 利用不同的listnode_handle類進行處理,

# 因為使用頭插法插入節點,正好每個節點都是倒序鏈結起來,方便了遍歷鍊錶結算每位數的相加

for i in l1_list:

l1 = listnode_1.add(i)

listnode_2 = listnode_handle(

) l2 = listnode(

) l2_list =[1

]for i in l2_list:

l2 = listnode_2.add(i)

# result = solution().addtwonumbers(l1, l2)

# result1_node1 = listnode_handle()

# des=result1_node1.get_list(result)

# 方法2 構建

result1 = solution1(

).addtwonumbers(l1, l2)

result1_node = listnode_handle(

) des=result1_node.get_list(result1)

print

(des[::

-1])

LeetCode 兩數相加

題目來自leetcode 注意幾點 鍊錶對應結點相加時增加前乙個結點的進製,並儲存下乙個結點的進製 兩個鍊錶長度不一致時,要處理較長鍊錶剩餘的高位和進製計算的值 如果最高位計算時還產生進製,則還需要新增乙個額外結點。definition for singly linked list.struct l...

leetcode 兩數相加

給定兩個非空鍊錶來表示兩個非負整數。位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將兩數相加返回乙個新的鍊錶。你可以假設除了數字 0 之外,這兩個數字都不會以零開頭.示例 輸入 2 4 3 5 6 4 輸出 7 0 8 原因 342 465 807 definition for singly ...

leetcode 兩數相加

給定兩個非空鍊錶來表示兩個非負整數。位數按照逆序方式儲存,它們的每個節點只儲存單個數字。將兩數相加返回乙個新的鍊錶。你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。示例 輸入 2 4 3 5 6 4 輸出 7 0 8原因 342 465 807 definition for singly l...