将链接列表中的节点与堆栈相反。为什么新链接列表变得如此之大?
我想在链接列表中使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个问题:
主要问题是
中附加的最后一个节点,而tmp_sk
循环仍将具有未重置的下一个参考。因此,当tmp_sk
完成时,您有一个链接列表,其最后一个节点是new_tail
,但是该节点不是真正的 a tail:it具有从未更新的Next
参考,并指的是最初是第二个节点的节点,现在是一个但持久的节点。因此,现在有一个周期。解决方案是执行new_tail.next = none
在该循环之后。内部循环不应具有
i&lt; 3
作为条件。这只有在您的列表有三个节点时才能起作用。您应该制作此通用,然后测试cur
。这也意味着该循环中的else
块永远不会执行。应该省略它。cur:毫无目的。一旦循环进行了一次迭代,它将始终退出。如果它会进行第二次迭代并使用新的堆栈等重新启动,那是没有意义的。因此,请删除该循环并保持其身体。
您打印列表的部分,还打印了虚拟节点的值。我不认为这是目的;所以跳过第一个节点。
不是问题,但是您应该将代码分为函数,其中每个功能负责一个任务:创建列表,倒转列表,打印列表。
这是对您的代码的更正和清理:
Several issues:
The main issue is that the last node that is appended in the
while tmp_sk
loop, will still have anext
reference that is not reset. So when thewhile tmp_sk
has finished, you have a linked list whose last node isnew_tail
, but that node is not really a tail: it has anext
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 performnew_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 forcur
instead. This also means that theelse
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: