简体   繁体   English

python链表后退并前进

[英]python Linked List move backwards and move forward

How can reorder the list so that it starts with the first occurrence of the smallest element and repeatively moves backwards by one step and forward by three steps? 如何对列表进行重新排序,使其从第一次出现的最小元素开始,并反复向后移动一个步骤,并向前移动三个步骤? I can only find the smallest element (for example ,I got 5 in the following test)? 我只能找到最小的元素(例如,在下面的测试中我得到了5个)? So how can I get the list (5, 53, 65, 33, 51, 62, 61, 38, 74, 45, 97, 49)? 那么如何获取列表(5、53、65、33、51、62、61、38、74、45、97、49)?

class ExtendedLinkedList(LinkedList):

    def __init__(self, L = None):
        super().__init__(L)

    def rearrange(self):
        node = self.head
        if not node:
            return None
        Min = node.value
        while node:
            if node.value < Min:
                Min = node.value
            node = node.next_node
        return Min


---------test---------

LLL = ExtendedLinkedList([49, 97, 53, 5, 33, 65, 62, 51, 38, 61, 45, 74])

LLL.print()

print(LLL.rearrange())

So you're doing better than what I assume is your fellow student . 所以你做得比我想你的同学好 You have the minimum part worked out. 您已完成了最少的工作。 But now here's the question: are you using a doubly-linked list (the sane way to do this) or a singly-linked list (the fun way)? 但是,现在的问题是:您使用的是双向链接列表(执行此操作的明智方式)还是单链接列表(有趣的方式)? And do you want to give a sane answer, or a fun answer? 您想给出一个合理的答案还是一个有趣的答案?

The difficulty abarnert is mentioning is that in order to find the minimum value, you have to go through the entire list and keep track of the value , but in order to then start from there in gathering values, you'll need to keep track of the node itself, because otherwise, when you get to the end of the list, you'll know what the minimum value was, but you'll have no way of getting back there! abarnert提到的困难是,要找到最小值,您必须遍历整个列表并跟踪该 ,但是要从那里开始收集值,则需要跟踪节点本身,因为否则,当您到达列表的末尾时,您将知道最小值是多少,但是您将无法返回该最小值! There are a few options here. 这里有一些选择。 One is that you can store both the min value and min node when going through. 一种是您可以同时存储最小值和最小值节点。 The other is that you can store just the node, and compare values of nodes. 另一个是您可以只存储节点,并比较节点的值。

Now, having done that, you'll have the minimum node to start from. 现在,完成此操作后,您将具有最小的起点。

The sane choice : you have a doubly linked list. 明智的选择 :您有一个双向链表。 Each node has a next_node that points to the next node, and a previous_node that points to the previous one. 每个节点都有一个next_node指向下一个节点,以及previous_node指向前一个。 Going back and forward is easy, and I don't think it needs much explanation. 前进和后退很容易,我认为不需要太多解释。

The fun choice : you have a singly linked list. 有趣的选择 :您有一个单链表。 You must always move forward! 您必须始终前进! But that's alright, if you make some slight changes. 但是没关系,如果您进行一些细微的更改。 When finding the minimum, instead of finding the minimum node, find the node before it; 当找到最小值时,而不是找到最小节点,而是找到它之前的节点; let's call it A. Now start making your rearranged list. 我们称它为A。现在开始制作重新排列的列表。 Do it by first putting on the value of the node after A, then putting the value of A. Now move forward two . 为此,首先将节点的值放在A后面,然后将A的值放在上面。现在向前移动2 Add the value of the node after that, then add that node. 添加节点的值之后 ,然后添加节点。 And so on... In this way you never actually have to move backward. 依此类推...这样,您实际上不必向后移动。


In writing this I did not notice the 97,49 at the end of your example. 在撰写本文时,我没有注意到示例末尾的97,49。 Do you need to loop around too? 您是否也需要循环播放? That can also be done in both cases. 在两种情况下都可以做到这一点。 You can just set the last node's next_node to the first node after the minimum finding part, and then when reordering, check to make sure you haven't gotten all the way back around. 您可以将最后一个节点的next_node设置为最小查找部分之后的第一个节点,然后在重新排序时检查以确保没有完全回到原来的位置。


So playing around with this, I managed to get the following rather compact code to work (I use nn instead of next_node and v instead of value ): 因此,我设法使以下紧凑的代码正常工作(我使用nn代替next_node ,使用v代替value ):

def reorder(f):
    n,a=f,f
    while n.nn:
        a,n=[a,n][n.nn.v<a.nn.v],n.nn
    n.nn,ff=f,a.nn
    while a:
        a.nn.nn,a.nn,a=[(a,None,None),(a,a.nn.nn.nn,a.nn.nn)][
            (a.nn==ff)or((a.nn.nn!=ff.nn)and(a.nn.nn.nn!=ff.nn))]
    return ff

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

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