q.next = self.head AttributeError:“ NoneType”对象没有属性“ next”

时间:2020-05-23 02:43:37

标签: python-3.x data-structures singly-linked-list

下面的代码是我尝试用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

我无法理解为什么会收到此错误。而在其他编译器(在线)上也可以正常工作。

0 个答案:

没有答案