繁体   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