Я ищу функцию, которая возвращает связанный список, который не содержит определенный узел.«Удаление» узла из функционального связанного списка
Вот пример реализации:
Nil = None # empty node
def cons(head, tail=Nil):
""" Extends list by inserting new value. """
return (head, tail)
def head(xs):
""" Returns the frst element of a list. """
return xs[0]
def tail(xs):
""" Returns a list containing all elements except the first. """
return xs[1]
def is_empty(xs):
""" Returns True if the list contains zero elements """
return xs is Nil
def length(xs):
"""
Returns number of elements in a given list. To find the length of a list we need to scan all of its
elements, thus leading to a time complexity of O(n).
"""
if is_empty(xs):
return 0
else:
return 1 + length(tail(xs))
def concat(xs, ys):
""" Concatenates two lists. O(n) """
if is_empty(xs):
return ys
else:
return cons(head(xs), concat(tail(xs), ys))
Как может remove_item
функция будет реализована?
Nil = Нет серьезно? –
Итак? Я думаю, что «Нет» является хорошим значением «Нил» во многих случаях. И использование 'Nil' вместо' None' в источнике объясняет больше. – Alfe
@ user2799617 Почему бы и нет? – Rob