今天刷LeetCode的一道链表逆序题,看到别人的solution后感到特别疑惑:
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p, r=head, None
while p:
r,p.next,p=p,r,p.next
return r
对于中间那个连续赋值过程不太理解,后来去搜索连续赋值的原理才知道
python连续赋值是元组封装和序列拆封过程
t = 12345, 54321, 'hello!'
x, y, z = t
相当于先取出,再赋值
a,b=b,a
上面的赋值可以理解为:
temp_b=b
temp_a=a
a=f_temp
b=f_temp