重排单向链表

问题描述

我无法理解我在网上找到的问题的部分解决方案。

给定一个单链表的头部,编写一个方法修改链表,使得链表后半部分的节点以相反的顺序交替插入到前半部分的节点中。所以如果 LinkedList 有节点 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null,你的方法应该返回 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null。

你的算法不应该使用任何额外的空间,输入的 LinkedList 应该就地修改

这是我的解决方案,与原来的非常相似。

from __future__ import print_function


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

  def print_list(self):
    temp = self
    while temp is not None:
      print(str(temp.value) + " ",end='')
      temp = temp.next
    print()


def reorder(head):
  if head is None or head.next is None:
    return

  slow,fast = head,head

  while fast is not None and fast.next is not None:
    slow = slow.next
    fast = fast.next.next

  head_second_half = reverse(slow)
  head_first_half = head

  while head_first_half is not None and head_second_half is not None:
    temp = head_first_half.next
    head_first_half.next = head_second_half
    head_first_half = temp

    temp = head_second_half.next
    head_second_half.next = head_first_half
    head_second_half = temp

  if head_first_half is not None:
    head_first_half.next = None

def reverse(head):
  prevIoUs = None

  while head is not None:
    _next = head.next
    head.next = prevIoUs
    prevIoUs = head
    head = _next

  return prevIoUs


def main():
  head = Node(2)
  head.next = Node(4)
  head.next.next = Node(6)
  head.next.next.next = Node(8)
  head.next.next.next.next = Node(10)
  head.next.next.next.next.next = Node(12)
  reorder(head)
  head.print_list()


main()

给我带来最大麻烦的是 reorder 中的第二个 while 循环。我有做以下事情的想法...

temp = head_first_half.next
head_first_half.next = head_second_half
head_first_half = temp

等等...

只有当我将最后一个节点的 next 设置为 None in 时,答案才正确

if head_first_half is not None:
  head_first_half.next = None

但是,我不得不偷看解决方案才能获得这部分内容。在纸上更仔细地研究这个问题后,我注意到我可能甚至不明白我在第二个 while 循环中自己写的内容reorder 中。我相信正在发生的是以下...

第一次迭代

temp -> 4 -> 6 -> 8 -> None
head_first_half -> 4 -> 6 -> 8 -> None
and the current linked list: 2 -> 12 -> 10 -> 8 -> None

temp -> 10 -> 8 -> None
head_second_half -> 10 -> 8 -> None
and the current linked list: 2 -> 12 -> 4 -> 6 -> 8 -> None

第二次迭代

temp -> 6 -> 8 -> None
head_first_half -> 6 -> 8 -> None
and the current linked list: 2 -> 12 -> 4 -> 10 -> 8 -> None

temp -> 8 -> None
head_second_half -> 8 -> None
and the current linked list: 2 -> 12 -> 4 -> 10 -> 6 -> 8 -> None

第三次迭代

temp -> 8 -> None
head_first_half -> 8 -> None
and the current linked list: 2 -> 12 -> 4 -> 10 -> 6 -> 8 -> None

temp -> None
head_second_half -> None
and the current linked list: 2 -> 12 -> 4 -> 10 -> 6 -> 8 -> 8 -> None

因为 head_second_half is None while 循环结束。 然后,检查是否 head_first_half is not None 然后 head_first_half.next = None

在第三次迭代中,我没有看到我们的结果如何......

2 -> 12 -> 4 -> 10 -> 6 -> 8 -> None

从我可以看到的 8 点回到自身,因为在第三次迭代中

head_first_half -> 8 -> None

并通过做

head_second_half.next = head_first_half

我认为 head_second_half.next == None 和之前的节点是 8,但现在有了 head_first_half,然后 head_second_half.next 指向 8 -> None。我知道我们没有复制链表中的任何内容,所以我可以理解的是,如果 8 -> None from head_first_half 不是一个单独的链表,而是一个指向 {{1} },那么,我们这里就没有循环了。但是,如果这不对(我的想法)那么我们不应该有一个循环吗?

还有为什么我们必须在 8 -> None 中执行最后一个 if 语句?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...