第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Python中合并兩個排序的鏈接列表時,“NoneType”對象沒有屬性“next”

在Python中合并兩個排序的鏈接列表時,“NoneType”對象沒有屬性“next”

BIG陽 2023-12-12 15:55:32
我用Python編寫了一個函數(shù)來合并兩個排序的鏈表。這是代碼:- class Node:    def __init__(self, data):        self.data = data        self.next = Noneclass LinkedList:    def __init__(self):        self.head = None    def append(self, data):        new_node = Node(data)        if self.head is None:            self.head = new_node            return        last_node = self.head        while last_node.next is not None:            last_node = last_node.next        last_node.next = new_node        def print(self):        cur_node = self.head        while cur_node.next is not None:            print(cur_node.data, "-> ", end='')            cur_node = cur_node.next        print(cur_node.data)def merge(l1, l2):    cur_node1 = l1.head    cur_node2 = l2.head    l3 = LinkedList()    while cur_node1.next is not None or cur_node2.next is not None:        if cur_node1.data > cur_node2.data:            l3.append(cur_node2.data)            cur_node2 = cur_node2.next        else:            l3.append(cur_node1.data)            cur_node1 = cur_node1.next    if cur_node1.next is None:        l3.append(cur_node1.data)        while cur_node2.next is not None:            l3.append(cur_node2.data)            cur_node2 = cur_node2.next        l3.append(cur_node2.data)    elif cur_node2.next is None:        l3.append(cur_node2.data)        while cur_node1.next is not None:            l3.append(cur_node1.data)            cur_node1 = cur_node1.next        l3.append(cur_node1.data)    return l3ll1 = LinkedList()ll2 = LinkedList()ll1.append(12)ll1.append(45)ll1.append(69)ll1.append(70)ll2.append(1)ll2.append(2)ll2.append(99)ll2.append(100)ll3 = merge(ll1, ll2)ll3.print()這里發(fā)生了什么?我不明白。我嘗試在合并函數(shù)的 while 循環(huán)中運行沒有 or 語句的代碼。效果很好。顯然問題出在 while 語句中。有人可以幫忙嗎?
查看完整描述

3 回答

?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

問題出現(xiàn)在您的第一個循環(huán)中:


while cur_node1.next is not None or cur_node2.next is not None:

這意味著兩個節(jié)點之一可以具有next屬性None。如果該節(jié)點的數(shù)據(jù)也小于另一個節(jié)點中的數(shù)據(jù),則該節(jié)點變量將設置為None。然后while再次評估條件,并產(chǎn)生錯誤,因為該None值沒有next屬性......


cur_nodeX.next通過檢查屬性而None不是其本身,實際上使算法變得過于復雜cur_nodeX。因此,在第一個循環(huán)完成后,您仍然需要從兩個列表中添加節(jié)點。此外,您的代碼假設兩個列表都不為空。


while因此,根據(jù)當前節(jié)點而不是它們的屬性來設置條件next:


def merge(l1, l2):

    cur_node1 = l1.head

    cur_node2 = l2.head

    l3 = LinkedList()

    while cur_node1 is not None or cur_node2 is not None:

        if cur_node1 is None or cur_node2 is not None and cur_node1.data > cur_node2.data:

            l3.append(cur_node2.data)

            cur_node2 = cur_node2.next

        else:

            l3.append(cur_node1.data)

            cur_node1 = cur_node1.next

    return l3


查看完整回答
反對 回復 2023-12-12
?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

對于任何想知道的人來說,這都是有效的。


def merge(l1, l2):

cur_node1 = l1.head

cur_node2 = l2.head

l3 = LinkedList()

while True:

    if cur_node1 is None:

        while cur_node2.next is not None:

            l3.append(cur_node2.data)

            cur_node2 = cur_node2.next

        l3.append(cur_node2.data)

        break

    elif cur_node2 is None:

        while cur_node1.next is not None:

            l3.append(cur_node1.data)

            cur_node1 = cur_node1.next

        l3.append(cur_node1.data)

        break

    if cur_node1.data > cur_node2.data:

        l3.append(cur_node2.data)

        cur_node2 = cur_node2.next

    elif cur_node1.data < cur_node2.data:

        l3.append(cur_node1.data)

        cur_node1 = cur_node1.next

return l3


查看完整回答
反對 回復 2023-12-12
?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

打印“1 -> 2 -> 12 -> 45 -> 69 -> 70 -> 99 -> 100”


def merge(l1, l2):

    cur_node1 = l1.head

    cur_node2 = l2.head

    l3 = LinkedList()

    while cur_node1 and (cur_node1.next is not None or cur_node2.next is not None):

        if cur_node1.data > cur_node2.data:

            l3.append(cur_node2.data)

            cur_node2 = cur_node2.next

        else:

            l3.append(cur_node1.data)

            cur_node1 = cur_node1.next

        if cur_node1.next is None:

           l3.append(cur_node1.data)

           while cur_node2.next is not None:

               l3.append(cur_node2.data)

               cur_node2 = cur_node2.next

           l3.append(cur_node2.data)

        elif cur_node2.next is not None:

            l3.append(cur_node2.data)

            while cur_node1.next is not None:

                l3.append(cur_node1.data)

                cur_node1 = cur_node1.next

            l3.append(cur_node1.data)

    return l3


查看完整回答
反對 回復 2023-12-12
  • 3 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號