下面是我从循环链表中删除任何节点的代码:
def delete_node(head, value):
p = head
if p is None:
return None
if p.value == value and p.next == p:
return None
while p.value != value:
p = p.next
if p.next is head and p.value != value:
return head
p.value = p.next.value
if p.next == head:
head = p
p.next = p.next.next
return head
pass
有没有其他方法可以从循环链表中删除一个节点?
def delete(head, value):
curr = head
prev = curr.next
while prev.next != head:
prev = prev.next
if curr.value == value:
prev.next = curr.next
curr = curr.next
prev = prev.next
while curr.value != value and curr != head:
curr = curr.next
prev = prev.next
if curr.value == value:
prev.next = curr.next