简体   繁体   English

递归反转链表中数字的顺序

[英]Reversing order of numbers in linked list recursivly

I want to reverse the order of numbers in a linked list with recursion.我想用递归反转链表中数字的顺序。

(1,(2,(3,( )))) ----> (3,(2,(1,( )))) (1,(2,(3,( )))) ----> (3,(2,(1,( ))))

My linked lists, are nested tuples.我的链表是嵌套元组。 First(xs) returns the first element, in the above example it would be either 1 or 3 on the right side. First(xs) 返回第一个元素,在上面的例子中,它在右侧是 1 或 3。 Rest(xs) returns all other elements, (2,(3,( ))) or (2,(1,( ) on the right side. Rest(xs) 返回所有其他元素,右侧的 (2,(3,( ))) 或 (2,(1,( ))。

my code looks like this:我的代码是这样的:

empty = ()
def reversed(xs):
    if xs == (first(xs), empty):
        return first(xs)
    else:
        return reversed(rest(xs)), first(xs)

but it yields in following output:但它产生以下输出:

((3, 2), 1)

I guess, I'm pretty close, but I'm running out of ideas how to fix this.我想,我已经很接近了,但是我已经没有办法解决这个问题了。

Could anyone help me?有人可以帮助我吗?

Thank you谢谢

There are different ways to do this.有不同的方法可以做到这一点。 Right now, you are merely concatenating them to a new tuple, which does not create a properly formed linked list.现在,您只是将它们连接到一个新元组,它不会创建一个正确格式的链表。 Instead, you could append the former first to the reversed rest using a second function.相反,您可以使用第二个函数first append前者appendreversed rest Also, your base-case seems to be wrong, as you return just a single element instead of a linked list.此外,您的基本情况似乎是错误的,因为您只返回一个元素而不是链表。

def reversed(xs):
    if xs == empty:
        return empty
    else:
        return append(reversed(rest(xs)), first(xs))

def append(xs, x):
    if xs == empty:
        return (x, empty)
    else:
        return first(xs), append(rest(xs), x)

>>> reversed((1, (2, (3, (4, empty)))))
(4, (3, (2, (1, ()))))

Note, however, that this has quadratic complexity O(n²) as you have to iterate the entire list (or partial list) to append the first node in each step of the reversed function.但是请注意,这具有二次复杂度 O(n²),因为您必须迭代整个列表(或部分列表)以在reversed函数的每个步骤中appendfirst节点。 For a linear complexity O(n) version, you could add a second paramter to your reversed function (or create a second function to be called by the original function if you can't change the signature).对于线性复杂度 O(n) 版本,您可以向reversed函数添加第二个参数(或者,如果无法更改签名,则创建由原始函数调用的第二个函数)。 This will build the reversed list in that second parameter and finally just return that list.这将在第二个参数中构建反向列表,最后只返回该列表。

def reversed(xs, tail=empty):
    if xs == empty:
        return tail
    else:
        return reversed(rest(xs), (first(xs), tail))

Also, as noted in comments, there are better languages for defining recursive data structures than Python, and better ways to use lists in Python, but I'll assume that this is just for learning and not for practical use.此外,正如评论中所指出的,有比 Python 更好的定义递归数据结构的语言,以及在 Python 中使用列表的更好方法,但我假设这仅用于学习而不是用于实际用途。

You can use simple recursion:您可以使用简单的递归:

d = (1,(2,(3,())))
def reverse(_d, _c = ()):
  a, b = _d
  return (a, _c) if not b else reverse(b, (a, _c))

print(reverse(d))

Output:输出:

(3, (2, (1, ())))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM