【问题标题】:Explanation about pointers vs manipulating objects in a linked list关于指针与在链表中操作对象的说明
【发布时间】:2020-04-10 22:58:44
【问题描述】:

我很难理解切换指针与实际操作链表中对象的概念。

这是从头开始构建链表的代码

class ListNode:

    def __init__(self, val:int, nextNode=None):
        self.val = val
        self.next = nextNode

class LinkedList:

    def __init__(self, val=None):

        self.head = None
        self.tail = None

    def addValue(self, val:int):
        if self.head == None:
            self.head = self.tail = ListNode(val)

        else:
            self.tail.next = ListNode(val)
            self.tail = self.tail.next
        return self.tail

    def addMultiple(self, values:list):

        for i in values:
            self.addValue(i)


    def add_to_beginning(self, val):
        if self.head == None:
            self.head = self.tail = ListNode(val)
        else:
            self.head = ListNode(val, self.head)

    def display(self):

        elems = []

        curr = self.head
        while curr:
            elems.append(curr.val)
            curr = curr.next

        print(elems)

在这里创建一个链表:

l1 = LinkedList()
l1.addMultiple([1,2,3,4,5])

例如,如果我想将第 n 个元素移动到头部,那么我创建了这个函数

class Solution:

    def move_n_to_head(self, head, n):

        if head == None:
            return None
        if head.next == None:
            return head

        temp = None
        count = 0
        dummy = fast = slow = ListNode(0)

        fast.next = head
        while fast:

            if count == n:
                temp = fast.next
                fast.next = fast.next.next #why does this line manipuate the head?
                break

            fast = fast.next #why does this line NOT manipulate the head?
            count += 1

        slow.next = temp
        slow.next.next = head
        return dummy.next

一切正常,我得到了我想要的解决方案,但特别是我不明白为什么这条线确实操纵头部?

fast.next = fast.next.next

在第三次迭代中使用上述行后,头部现在变为 [1,2,3,5]

但是,当我遍历列表时,这条线 操纵头部?每次迭代后头部仍然是[1,2,3,4,5]?

fast = fast.next

我阅读了关于虚拟节点指针的其他 stackoverflow 解释,这很有帮助,但我仍然不明白。

Explanation about dummy nodes and pointers in linked lists

提前致谢!

【问题讨论】:

    标签: python object pointers linked-list nodes


    【解决方案1】:

    首先,很高兴看到这样清晰易读的代码。现在,以下行不起作用的原因

    fast.next = head #fast is an object.
    

    是因为你将 fast 声明为一个对象

    dummy = fast = slow = ListNode(0) #just here
    

    会发生什么?

    在 python 中,你使用的是对象而不是指针(指针真的不是那样,它们是引用。

    有时传递或创建的某些变量被解释为对象,有时被解释为指针(引用)。

    好吧,你会看到:

    Python 不需要指针来实现这一点,因为每个变量都是对对象的引用。这些引用与 C++ 引用略有不同,因为它们可以被分配给 - 很像 C++ 中的指针。 (obmarg)

    很难告诉你如何让语言将变量解释为对象引用。

    如果我想好了,在这种情况下可能会通过以下修改来完成

    class Solution:
    
        def move_n_to_head(self, head, n):
    
            if head == None:
                return None
            if head.next == None:
                return head
    
            temp = None
            count = 0
            dummy = slow = ListNode(0)
    
            fast = head #fast now "points" to head 
            # (it depends if it is taken as a copy of head or a reference to it)
            while fast:
    
                if count == n:
                    temp = fast.next
                    fast.next = fast.next.next #It should be solved
                    break
    
                fast = fast.next
                count += 1
    
            slow.next = temp
            slow.next.next = head
            return dummy.next
    

    注意:

    This post 讨论如何在 python 中实现指针,并且可能会为您提供有关如何处理它们的更多信息。

    我真的希望它对你有所帮助,感谢您阅读此答案。

    【讨论】:

    • 感谢您抽出宝贵时间回答这个问题!我仍然不明白为什么 fast = fast.next 不操纵头部,而 fast.next = fast.next.next 确实操纵头部。我已经用一个例子编辑了我上面的问题。谢谢!
    猜你喜欢
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多