특정 노드가없는 링크 된 목록을 반환하는 함수를 찾고 있습니다. 기능 링크 된 목록에서 노드 제거 "
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 = 심각하지 않습니까? –
그럼요? 나는 많은 경우에'None'이 좋은'Nil' 값이라고 생각합니다. 그리고 소스에서'None' 대신'Nil'을 사용하는 것이 더 설명이됩니다. – Alfe
@ user2799617 왜 안 되니? – Rob