Python でリンク リスト クラスを作成しようとしています (無意味ですが、学習課題です)。リンク リストの最初の要素を削除しようとすると、ノードを削除するために記述したメソッドが機能しません。 . 削除するノードがリンク リスト内の他の場所にある場合、このメソッドは正常に機能します。どこが間違っているのか、誰かが私に洞察を与えることができますか?
これまでの私のコードは次のとおりです。
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __repr__(self):
return repr(self.data)
def printNodes(self):
while self:
print self.data
self = self.next
def removeNode(self, datum):
"""removes node from linked list"""
if self.data == datum:
return self.next
while self.next:
if self.next.data == datum:
self.next = self.next.next
return self
self = self.next