簡體   English   中英

混淆鏈表指針以及它如何改變原始鏈表

[英]Confused with Linked List Pointers and how it changes the original linked list

我正在研究數據結構和算法。 我在下面遇到了一些困惑。

from typing import *

class Node:
    curr_node_value: Any
    next_node: Node
    def __init__(self, curr_node_value: Any = None):
        # a node can hold a current value and by default its next node is None
        # however we can assign values to the next of a node, but the next must be of object node as denoted
        # note the distinction between curr node value and next node, they are diff
        self.curr_node_value = curr_node_value
        self.next_node = None
        
class LinkedList:
    def __init__(self):
        # key point is that end of every llist, it points to None always
        self.head = None
        
        
ll1_first = Node(1)
ll1_second = Node(2)
ll1_third = Node(4)

ll2_first = Node(1)
ll2_second = Node(3)
ll2_third = Node(4)

# create llist 1
ll1 = LinkedList()
ll1.head = ll1_first
ll1.head.next_node = ll1_second
ll1.head.next_node.next_node = ll1_third

# create llist 2
ll2 = LinkedList()
ll2.head = ll2_first
ll2.head.next_node = ll2_second
ll2.head.next_node.next_node = ll2_third


merged_sorted_llist = LinkedList()

ll1_temp_curr_node = ll1.head
ll2_temp_curr_node = ll2.head

while ll1_temp_curr_node is not None and ll2_temp_curr_node is not None:
    print(ll1_temp_curr_node.curr_node_value)
    ll1_curr_node = ll1_temp_curr_node
    ll2_curr_node = ll2_temp_curr_node
 
    if ll1_curr_node.curr_node_value <= ll2_curr_node.curr_node_value:
        merged_sorted_llist.head = ll1_curr_node
        #print(merged_sorted_llist.head.next_node.curr_node_value)
        merged_sorted_llist.head.next_node = ll2_curr_node
 

    ll1_temp_curr_node = ll1_temp_curr_node.next_node
    ll2_temp_curr_node = ll2_temp_curr_node.next_node

該代碼是一個更大問題的子集,但我無法清楚地理解為什么當您打印print(ll1_temp_curr_node.curr_node_value)它給您 1、1、3 而不是 1、2、4。我進行了廣泛的調試,如果您注釋掉merged_sorted_llist.head.next_node = ll2_curr_node它將打印出 1、2、4。

谷歌搜索后,我開始閱讀這篇文章並相信我在變量分配的某個地方搞砸了,特別是如果我設置了屬性。 對我來說,它在哪里仍然不是很明顯。

我可以看到兩個問題:

  1. 您不想在每次迭代中更新合並列表的頭部
  2. 一旦分配了合並列表的頭部,您仍然需要通過比較兩個候選節點(每個列表中的一個)來找到下一個節點

這是我對這個問題的實現:

# Linked List Node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
  
  
# Create & Handle List operations
class LinkedList:
    def __init__(self):
        self.head = None
  
    # Method to display the list
    def printList(self):
        temp = self.head
        while temp:
            print(temp.data, end=" ")
            temp = temp.next
    
    
  
    # Method to add element to list
    def addToList(self, newData):
        newNode = Node(newData)
        if self.head is None:
            self.head = newNode
            return
  
        last = self.head
        while last.next:
            last = last.next
  
        last.next = newNode

# Create 2 lists
listA = LinkedList()
listB = LinkedList()
  
# Add elements to the list in sorted order
listA.addToList(5)
listA.addToList(10)
listA.addToList(15)
  
listB.addToList(2)
listB.addToList(3)
listB.addToList(20)

def mergeLists(h1, h2):
    merged_head = None
    
    if h1 is None:
        return h2
    
    if h2 is None:
        return h1
    
    if h1.data > h2.data:
        merged_head = h2
        merged_head.next = mergeLists(h1, h2.next)
    else:
        merged_head = h1
        merged_head.next = mergeLists(h1.next, h2)

    return merged_head

res = LinkedList()
res.head = mergeLists(listA.head, listB.head)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM