Я пытаюсь реализовать алгоритм для удаления дубликатов из связанного списка, но мой алгоритм замораживается, когда дело доходит до того, что текущий узел имеет данные, равные следующему.Linkedlist Удалить дубликаты без буфера
class Node:
def __init__(self, data, next):
self.data = data
self.next = next
class LinkedList:
def __init__(self, head = None):
self.head = head
def add(self, item):
newNode = Node(item, self.head)
self.head = newNode
def printit(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
def removeDuplicates(self):
current = self.head
while current != None:
runner = current
while runner.next != None:
if runner.next.data == current.data:
runner.next = current.next.next
else:
runner = current.next
current = current.next
mylist = LinkedList()
mylist.add(31)
mylist.add(77)
mylist.add(31)
mylist.add(22)
mylist.add(22)
mylist.add(22)
mylist.printit()
mylist.removeDuplicates()
mylist.printit()
Возможно, это действительно глупо, но я не могу определить его прямо сейчас, любые идеи?
'next' - это ключевое слово python. Используйте другое имя, чтобы избежать путаницы. – kmad1729