def print_list(linked_list): # Non-recursive outer function. You might want
_print_list_helper(linked_list.first) # to update it to handle empty lists nicely!
def _print_list_helper(node): # Recursive helper function, gets passed a
if node is not None: # "node", rather than the list wrapper object.
print(node.item)
_print_list_helper(node.next) # Base case, when None is passed, does nothing
def print_linked_list(item):
# base case
if item == None:
return
# lets print the current node
print(item.item)
# print the next nodes
print_linked_list(item.next)
看起来你的链接列表有两部分。列表节点具有
next
和item
属性,包装对象具有指向first
节点的属性。要递归打印列表,需要有两个函数,一个用于处理包装器,另一个用于对节点进行递归处理。试试这个。
你可以这样做:
相关问题 更多 >
编程相关推荐