【问题标题】:linked list beginning and end in pythonpython中的链表开始和结束
【发布时间】:2014-03-23 10:27:31
【问题描述】:

我的问题是我有一个 1->2->3->4->5 的链表

我的概念是将它们打印为 1->5->2->4->3

表示先开始后结束

我该怎么做?

我有一个想法,首先我在空节点中取一个空节点,我将保留开始节点和

之后,我将遍历到最后一个,并且结束节点将保留在那里,我的大脑停止了

谁能指导我这样做?提前致谢

def mutate_linked_list(head):
   #node creation
   s = t = Node()
   t.next = head
   head = head.next
   while head is None:
        t.next = head
        head = head.next
        # here i used recursion concept
        mutate_linked_list(head)
  return head

但它不起作用....

【问题讨论】:

  • 你必须使用链表吗?链表在进行随机访问时表现非常糟糕。双向链表可能是更好的选择(对于您的情况)。
  • 言归正传,你有理由不使用 Python 的内置列表类型吗?
  • 这不是pythonic的方式。不要这样做,使用列表。

标签: python list linked-list


【解决方案1】:
[head[0]]+[head[-1]] + head[1:-1]

【讨论】:

  • 你能写出完整的代码吗,请提前谢谢?
  • @pradeepkumar:这是假设head = [1, 2, 3, 4, 5]的完整代码
  • 这样写会更好head[:1] + head[-1:] + head[1:-1]IMO
  • 但是这个解决方案确实返回 [1,5,2,3,4] 而根据 OP 的预期输出将是 [1,5,2,4,3]
【解决方案2】:
def mutate_linked_list(head):
    # return early for an size 1 list
    if head.next is None:
        return head

    # find the last and penultimate nodes
    penultimate = head
    while penultimate.next.next is not None:
        penultimate = penultimate.next
    last = penultimate.next

    # detach the last node from the end
    penultimate.next = None

    # prepend it to the second node
    last.next = head.next.next

    # and reconnect the head
    head.next = last

    return head

【讨论】:

    【解决方案3】:
    from collections import deque
    def mutate_linked_list(head):
       mydeque = deque(head)
       try:
         while True:
           yield mydeque.popleft()
           yield mydeque.pop()
       except IndexError:
         pass
    
    for i in mutate_linked_list(range(1,6)):
      print(i)
    

    你显然可以做一个列表并 .append 到它上面而不是产生值。

    【讨论】:

      【解决方案4】:

      作为一个练习......

      def mix(head):
          # Walk to the end of the list
          last = head
          secondlast = None
          while last.next:
              secondlast = last
              last = last.next
      
          if secondlast:
               # move last element to second place
               secondlast.next = None
               last.next = head.next
               head.next = last
      
               # mix the rest of the list (recursion)
               mix(last.next)
      

      【讨论】:

        【解决方案5】:

        怎么样:

        from itertools import izip_longest
        def mutate_linked_list(head):
            m = len(head)/2 # Get the middle position
            return [item for pair in izip_longest(head[:m+1], head[:m:-1])
                         for item in pair
                         if item is not None]
        

        然后,你准备好了:

        head = [1,2,3,4,5]
        mutate_linked_list(head)
        
        [1, 5, 2, 4, 3]
        

        它也适用于具有偶数个元素的列表...

        head = [1,2,3,4,5,6]
        mutate_linked_list(head)
        
        [1, 6, 2, 5, 3, 4]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-04
          • 2012-04-14
          • 2017-03-09
          • 2018-11-03
          • 1970-01-01
          相关资源
          最近更新 更多