【问题标题】:python linkedlist partly reversepython链表部分反转
【发布时间】:2017-09-20 18:42:08
【问题描述】:

我在课堂上有一个链表,比如

7,5,3,1,2,4,6,8

我想得到类似的输出

1,3,5,7,2,4,6,8

当我尝试使用下面的反向代码时:

class Linkedlist:
def __init__(self, L = None):
...
def partlyreverse(self):
    tail = self.head
    current_node = self.head.next_node
    tail.next_node = None
    while current_node.value % 2 == 1:
        next_current_node = current_node.next_node
        current_node.next_node = tail
        tail = current_node
        current_node = next_current_node

    self.head = tail
L = Linkedlist([7,5,3,1,2,4,6,8])
L.partlyreverse()

我只能得到

1,3,5,7

那我该怎么做呢?

谢谢!

【问题讨论】:

  • 尝试将所有值存储在一个集合中并执行以下操作:sorted([odd for odd in list1 if odd % 2 != 0 ]) + sorted([even for even in list1 if even % 2 == 0]),这将返回一个包含您想要的值的列表。
  • reverse 是什么意思?我真的看不出你在你的例子中颠倒了什么地方。它看起来更像是对列表进行排序,并将之前的列表分成偶数和奇数列表。
  • 我想通过使用链表而不是链表来解决这个问题......无论如何,谢谢!

标签: python list linked-list reverse


【解决方案1】:

您的问题不是 100% 清楚:您是否试图将链表分成两半,开头为奇数,结尾为偶数?您是否尝试在不考虑值本身的情况下对列表进行分区?

此外,您的问题中一定有您尚未提交的代码......我已经对代码的外观做出了最好的猜测。

下面的 sn-p 返回你想要的输出:

class Node:
    def __init__(self, value):
        self.value = value
        self.next_node = None

class Linkedlist:
    def __init__(self, L=None):
        self.head = Node(L[0])
        current = self.head
        for value in L[1:]:
            current.next_node = Node(value)
            current = current.next_node

    def partlyreverse(self):
        tail_start = self.head
        tail_end = self.head
        tail2_start = None
        tail2_end = None

        current_node = self.head.next_node

        while current_node != None:
            if current_node.value % 2 == 1:
                next_current_node = current_node.next_node
                current_node.next_node = tail_start
                tail_start = current_node
                current_node = next_current_node
            else:
                if not tail2_start:
                    tail2_start = current_node
                    tail2_end = tail2_start
                    current_node = current_node.next_node             
                else:
                    tail2_end.next_node = current_node
                    tail2_end = current_node
                    current_node = current_node.next_node 

        tail_end.next_node = tail2_start
        self.head = tail_start

L = Linkedlist([7,5,3,1,2,4,6,8])

L.partlyreverse()

def RecursivelyPrintNodes(node):
    if node != None:
        print node.value
        RecursivelyPrintNodes(node.next_node)

RecursivelyPrintNodes(L.head)

# 1,3,5,7,2,4,6,8

最后,我看到了您在代码中的用途,但请注意,您编写的代码无法编译(您的类中存在缩进错误)。

【讨论】:

  • 太棒了;我的荣幸! :) 不要忘记“接受”答案——这样其他人就会知道你的问题已经解决了@qiancheng​​span>
【解决方案2】:

您基本上需要检查您的列表的开头是否包含奇数或偶数!并基于此,您需要反转!

>>> s
(7, 3, 5, 1, 2, 4, 6, 8)
>>> sorted(filter(lambda x:(x%2!=0 and s[0]%2!=0) or (x%2==0 and s[0]%2==0),s)) + sorted(filter(lambda x:(x%2!=0 and s[-1]%2!=0) or (x%2==0 and s[-1]%2==0),s))
[1, 3, 5, 7, 2, 4, 6, 8]

【讨论】:

  • 好吧,我试过了,但是LinkedList对象没有'sorted'属性,我想重新连接linkedlist中的节点而不是将其转换为列表...
猜你喜欢
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多