1. 首页
  2. 大数据
  3. 算法与数据结构
  4. Python单向循环链表节点删除

Python单向循环链表节点删除

上传者: 2024-06-17 11:27:25上传 PY文件 7.45KB 热度 33次
def delete(self, data):
    if not self.head:
        return

    if self.head.data == data:
        current = self.head
        while current.next != self.head:
            current = current.next
        if current == self.head:
            self.head = None
        else:
            current.next = self.head.next
        self.head = current.next
    else:
        current = self.head
        while current.next != self.head:
            if current.next.data == data:
                current.next = current.next.next
                return
            current = current.next
用户评论