Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

翻转链表 #82

Open
long250038728 opened this issue Oct 17, 2018 · 3 comments
Open

翻转链表 #82

long250038728 opened this issue Oct 17, 2018 · 3 comments

Comments

@long250038728
Copy link

def reverse(head):
reverse_head = None
current = head
while current:
reverse_head,reverse_head.next,current = current,reverse_head,current.next

为什么
reverse_head = current
reverse_head.next = reverse_head
current = current.next
这样会出错

@jerryderry
Copy link
Contributor

因为第一句reverse_head变成了current,这样第二句就成了reverse_head.next = current

@long250038728
Copy link
Author

因为很多其他语言不像python一样可以连续赋值,那么代码应该如何写,求赐教

@jerryderry
Copy link
Contributor

赐教不敢当。
你只需要用一个tmp变量保存之前的reverse_head就行了,因为你的目的是
reverse_head = current
reverse_head.next = 之前的reverse_head

python这里其实相当于在等号右边帮你创建了一个临时的tuple,然后这个临时的变量再依次赋给左边那些。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants