開始使用免費開始

從連結串列移除第一個節點

在前一個練習中,你已經學會如何在連結串列的開頭插入一個節點。

在本練習中,你要為 remove_at_beginning() 方法準備程式碼。為了達成這件事,你需要把連結串列的 head 指向 head 的下一個節點。

回顧一下 Node() 類別:

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

本練習屬於課程

Data Structures and Algorithms in 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.____ = ____
編輯並執行程式碼