反向 LinkedList 的嵌套输出
Nested output from reversing LinkedList
如何在尝试构建完全递归的 LinkedList 反转时避免嵌套输出 [(((4, 3), 2), 1)]
?我想将 [4, 3, 2, 1)]
作为输出,并想知道这在 Python 中是否可行而无需迭代最终结果。
class Leaf:
def __init__(self, value):
self.value = value
self.next_node = None
def set_next(self, next_node):
self.next_node = next_node
class LinkedList:
def __init__(self, list_init):
self.len = len(list_init)
self.node = Leaf(list_init[0])
self.node.set_next(build_node(list_init[1:]))
def print(self):
print("{}".format(self.node.value))
next_node = self.node.next_node
while(next_node is not None):
print("{}".format(next_node.value))
next_node = next_node.next_node
def build_node(list_nodes):
if len(list_nodes) > 1:
node = Leaf(list_nodes[0])
node.set_next(build_node(list_nodes[1:]))
else:
node = Leaf(list_nodes[0])
return node
linkedList = LinkedList(list_init = [1, 2, 3, 4])
def reverse_linked_list(linkedList):
first_node = linkedList.node
def _reverse_linked_list(node):
print("Enter: {}".format(node.value))
if node.next_node is not None:
print("Call: {}".format(node.next_node.value))
return _reverse_linked_list(node.next_node), node.value
return node.value
return [_reverse_linked_list(first_node)]
reverse_linked_list(linkedList)
我认为您可以将您的函数更新为:
def reverse_linked_list(linkedList):
first_node = linkedList.node
def _reverse_linked_list(node):
print("Enter: {}".format(node.value))
if node.next_node is not None:
print("Call: {}".format(node.next_node.value))
prev_value = _reverse_linked_list(node.next_node)
if isinstance(prev_value,int):
prev_value = (prev_value,)
return (*prev_value, node.value)
return node.value
return [*_reverse_linked_list(first_node)]
reverse_linked_list(linkedList)
如何在尝试构建完全递归的 LinkedList 反转时避免嵌套输出 [(((4, 3), 2), 1)]
?我想将 [4, 3, 2, 1)]
作为输出,并想知道这在 Python 中是否可行而无需迭代最终结果。
class Leaf:
def __init__(self, value):
self.value = value
self.next_node = None
def set_next(self, next_node):
self.next_node = next_node
class LinkedList:
def __init__(self, list_init):
self.len = len(list_init)
self.node = Leaf(list_init[0])
self.node.set_next(build_node(list_init[1:]))
def print(self):
print("{}".format(self.node.value))
next_node = self.node.next_node
while(next_node is not None):
print("{}".format(next_node.value))
next_node = next_node.next_node
def build_node(list_nodes):
if len(list_nodes) > 1:
node = Leaf(list_nodes[0])
node.set_next(build_node(list_nodes[1:]))
else:
node = Leaf(list_nodes[0])
return node
linkedList = LinkedList(list_init = [1, 2, 3, 4])
def reverse_linked_list(linkedList):
first_node = linkedList.node
def _reverse_linked_list(node):
print("Enter: {}".format(node.value))
if node.next_node is not None:
print("Call: {}".format(node.next_node.value))
return _reverse_linked_list(node.next_node), node.value
return node.value
return [_reverse_linked_list(first_node)]
reverse_linked_list(linkedList)
我认为您可以将您的函数更新为:
def reverse_linked_list(linkedList):
first_node = linkedList.node
def _reverse_linked_list(node):
print("Enter: {}".format(node.value))
if node.next_node is not None:
print("Call: {}".format(node.next_node.value))
prev_value = _reverse_linked_list(node.next_node)
if isinstance(prev_value,int):
prev_value = (prev_value,)
return (*prev_value, node.value)
return node.value
return [*_reverse_linked_list(first_node)]
reverse_linked_list(linkedList)