一个用元组表示的链表 Python

huangapple go评论126阅读模式
英文:

A linked list as a tuple Python

问题

给定一个已排序的元组链表(以元组形式表示,其中元组包含一个数字和指向下一个元组的链接),需要实现一个反转链表的函数。下面是一个示例输入和输出:

输入:
x = (1, (3, (6, (8, None))))

输出:
(8, (6, (3, (1, None))))

以下是一个修改后的函数,可以正确反转链表:

def reverse(linked_list: tuple):
    current_pair = linked_list
    reversed_list = None

    while current_pair:
        value, rest_of_list = current_pair
        new_pair = (value, reversed_list)
        reversed_list = new_pair
        current_pair = rest_of_list

    return reversed_list

使用这个函数,你可以将链表反转,得到正确的结果。

英文:

A connected list is given, which is implemented as a tuple (a number, link to the following pair) of the form in which the values are already sorted:
x = (1, (3, (4, (7, (9, None)
It is necessary to implement a function that reverses the list:
example of a call:
reverse((1, (3, (6, (8, None)))))
Result:
(8, (6, (3, (1, None))))

This is what i've done, i know it's incorrect cause first element would be doubled then

def reverse(linked_list: tuple):
    last_pair = (linked_list[0], None)
    while linked_list[1]:
        new_list = (linked_list[0], last_pair)
        return new_list
        return reverse(linked_list[1])

This is the result:
(1, (1, None))

I have no idea how to do it in correct way, there is nothing about linked lists as tuples on the internet

答案1

得分: 0

在问题中的实现没有评估传递给 reverse() 的所有部分。

以下是实现你的目标的一种方式:

def reverse(t):
    result = None
    while True:
        a, b = t
        result = a, result
        if (t := b) is None:
            break
    return result

print(reverse((1, (3, (6, (8, None))))))

**输出:**

(8, (6, (3, (1, None))))
英文:

The implementation in the question does not evaluate all parts of the argument passed to reverse().

Here's one way to achieve your objective:

def reverse(t):
    result = None
    while True:
        a, b = t
        result = a, result
        if (t := b) is None:
            break
    return result
    
print(reverse((1, (3, (6, (8, None))))))

Output:

(8, (6, (3, (1, None))))

答案2

得分: 0

如果您为"reverse"序列提供了额外的参数,您还可以通过递归函数成功地完成:

def reverse(linked_t: tuple, rev_seq=None):
    while linked_t[1] is not None:
        rev_seq = (linked_t[0], rev_seq)
        return reverse(linked_t[1], rev_seq)
    else:
        return linked_t[0], rev_seq

print(reverse((1, (3, (6, (8, None))))))

----------

(8, (6, (3, (1, None))))
英文:

If you supply an additional argument for "reverse" sequence you can also succeed with recursive function:

def reverse(linked_t: tuple, rev_seq=None):
    while linked_t[1] is not None:
        rev_seq = (linked_t[0], rev_seq)
        return reverse(linked_t[1], rev_seq)
    else:
        return linked_t[0], rev_seq


print(reverse((1, (3, (6, (8, None))))))

(8, (6, (3, (1, None))))

huangapple
  • 本文由 发表于 2023年1月9日 02:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定