将链接列表中的节点与堆栈相反。为什么新链接列表变得如此之大?

发布于 2025-01-25 23:29:18 字数 901 浏览 3 评论 0原文

我想在链接列表中使用stack反向节点。因此,我首先创建一个链接列表:

head = ListNode(0)
temp = head
temp.next = ListNode(1)
temp = temp.next
temp.next = ListNode(2)

然后,我使用stack存储列表节点,然后将它们弹出到新的链接列表 new_tail 以反转给定的给定链接列表。

dummy_node = ListNode(0)
cur = head
new_tail = dummy_node
while cur:
    i = 0
    tmp_sk = [] # use the stack to store the elements of linked list
    tmp_tail = cur 
    while i < 3:        
        if cur:
            tmp_sk.append(cur)
            cur = cur.next
            i +=1
        else:
            new_tail.next = tmp_tail
            break
    while tmp_sk:
        new_tail.next = tmp_sk.pop()
        new_tail = new_tail.next

但是发生了一些奇怪的事情。当我尝试打印新的链接列表时,列表非常大。

count = 0
new = dummy_node
while new:
    count+=1
    print(new.val)
    new = new.next

计数可能是一个大数字,并且打印无法阻止我干预。 我找不到问题所在。

I want to reverse nodes in a linked list with stack.so I first create a linked list:

head = ListNode(0)
temp = head
temp.next = ListNode(1)
temp = temp.next
temp.next = ListNode(2)

then I use stack to store the list nodes, then pop them to a new linked list new_tail to reverse the given linked list.

dummy_node = ListNode(0)
cur = head
new_tail = dummy_node
while cur:
    i = 0
    tmp_sk = [] # use the stack to store the elements of linked list
    tmp_tail = cur 
    while i < 3:        
        if cur:
            tmp_sk.append(cur)
            cur = cur.next
            i +=1
        else:
            new_tail.next = tmp_tail
            break
    while tmp_sk:
        new_tail.next = tmp_sk.pop()
        new_tail = new_tail.next

but something strange happened. when I try to print the new linked list, the list is very large.

count = 0
new = dummy_node
while new:
    count+=1
    print(new.val)
    new = new.next

the count can be a large number and the print can't stop util I intervene.
I couldn't find where the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2025-02-01 23:29:18

几个问题:

  • 主要问题是中附加的最后一个节点,而tmp_sk循环仍将具有未重置的下一个参考。因此,当tmp_sk完成时,您有一个链接列表,其最后一个节点是new_tail,但是该节点不是真正的 a tail:it具有从未更新的Next参考,并指的是最初是第二个节点的节点,现在是一个但持久的节点。因此,现在有一个周期。解决方案是执行new_tail.next = none在该循环之后。

  • 内部循环不应具有i&lt; 3作为条件。这只有在您的列表有三个节点时才能起作用。您应该制作此通用,然后测试cur。这也意味着该循环中的else块永远不会执行。应该省略它。

  • cur:毫无目的。一旦循环进行了一次迭代,它将始终退出。如果它会进行第二次迭代并使用新的堆栈等重新启动,那是没有意义的。因此,请删除该循环并保持其身体。

  • 您打印列表的部分,还打印了虚拟节点的值。我不认为这是目的;所以跳过第一个节点。

  • 不是问题,但是您应该将代码分为函数,其中每个功能负责一个任务:创建列表,倒转列表,打印列表。

这是对您的代码的更正和清理:

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None

def createList(values):
    # initialise list (generic - from a list of values)
    node = dummy = ListNode(None)
    for val in values:
        node.next = ListNode(val)
        node = node.next
    return dummy.next

def reverseList(head):
    cur = head
    tmp_sk = [] # use the stack to store the elements of linked list
    while cur:
        tmp_sk.append(cur)
        cur = cur.next

    dummy_node = ListNode(None)
    new_tail = dummy_node
    while tmp_sk:
        new_tail.next = tmp_sk.pop()
        new_tail = new_tail.next
    new_tail.next = None # this was missing!
    return dummy_node.next  # return the new head

def printList(head):
    cur = head
    while cur:
        print(cur.val)
        cur = cur.next

# main program
head = createList([0, 1, 2])
head = reverseList(head)
printList(head)

Several issues:

  • The main issue is that the last node that is appended in the while tmp_sk loop, will still have a next reference that is not reset. So when the while tmp_sk has finished, you have a linked list whose last node is new_tail, but that node is not really a tail: it has a next reference that never got updated, and refers to the node that was originally the second node, and is now the one-but-last node. And so there is a cycle now. The solution is to perform new_tail.next = None right after that loop.

  • The inner loop should not have i < 3 as condition. This will only work when your list has three nodes. You should make this generic, and test for cur instead. This also means that the else block in that loop will never be executed. It should just be omitted.

  • The outer loop while cur: serves no purpose. Once that loop has made one iteration, it will always exit. It wouldn't make sense if it would make a second iteration and restart with a new stack, etc. So remove that loop and just keep its body.

  • The part where you print the list, also prints the value of the dummy node. I don't suppose that is the purpose; so skip that first node.

  • Not a problem, but you should divide your code into functions, where each function is responsible for one task: create the list, reverse the list, print the list.

Here is the correction and clean up of your code:

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None

def createList(values):
    # initialise list (generic - from a list of values)
    node = dummy = ListNode(None)
    for val in values:
        node.next = ListNode(val)
        node = node.next
    return dummy.next

def reverseList(head):
    cur = head
    tmp_sk = [] # use the stack to store the elements of linked list
    while cur:
        tmp_sk.append(cur)
        cur = cur.next

    dummy_node = ListNode(None)
    new_tail = dummy_node
    while tmp_sk:
        new_tail.next = tmp_sk.pop()
        new_tail = new_tail.next
    new_tail.next = None # this was missing!
    return dummy_node.next  # return the new head

def printList(head):
    cur = head
    while cur:
        print(cur.val)
        cur = cur.next

# main program
head = createList([0, 1, 2])
head = reverseList(head)
printList(head)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文