下面的代码是我尝试用k旋转单个链接列表的代码。
def rotate(self, k):
p=self.head
q=self.head
prev=None
count=0
while p and count<k:
prev=p
p=p.next
count+=1
p=prev
while q:
prev=q
q=q.next
q=prev
q.next=self.head
self.head=p.next
p.next=None
return head
我无法理解为什么会收到此错误。而在其他编译器(在线)上也可以正常工作。