[英]Why does this reverse list python code return the reversed list?
def reverseList(self, head):
cur, prev = head, None
while cur:
cur.next, prev, cur = prev, cur, cur.next
return prev
This is the python code which return the reverse order linked list like below.这是返回反向顺序链表的python代码,如下所示。
Input: 1 -> 2 -> 3 -> Null输入:1 -> 2 -> 3 -> 空
Output: 3 -> 2 -> 1 -> Null输出:3 -> 2 -> 1 -> 空
The question is when I hand write the output with input, it comes out below process.问题是当我用输入手写输出时,它会出现在过程之下。
At the first while loop
cur.next = N,
prev = 1,
cur = 2
At the Second while loop
cur.next = 1,
prev = 2,
cur = 3
At the third while loop
cur.next = 2,
prev = 3,
cur = N
return 3
I guess it should return only 3, not 3 -> 2 -> 1-> Null.我想它应该只返回 3,而不是 3 -> 2 -> 1-> Null。 Why this code return the reverse order Linked list?
为什么此代码返回反向顺序链表? Could anyone answer?
有人可以回答吗?
You already wrote the answer yourself:你已经自己写了答案:
cur = 3
At the third while loop
cur.next = 2,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.