开始使用免费开始使用

从链表中移除第一个节点

在上一个练习中,您学习了如何在链表的开头插入一个节点。

在本练习中,您将为 remove_at_beginning() 方法准备代码。为此,您需要将链表的 head 指向 head 的下一个节点。

回顾 Node() 类:

class Node:
  def __init__(self, data):
    self.data = data
    self.next = None

本练习是课程的一部分

Python 中的数据结构与算法

查看课程

练习说明

  • remove_at_beginning() 方法中,将 head 指向 headnext 节点。

交互式实操练习

通过完成这段示例代码来试试这个练习。

class LinkedList:
  def __init__(self):
    self.head = None
    self.tail = None
    
  def remove_at_beginning(self):
    # The "next" node of the head becomes the new head node
    self.____ = ____
编辑并运行代码