微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重排单向链表

如何解决重排单向链表

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

给定一个单链表的头部,编写一个方法修改链表,使得链表后半部分的节点以相反的顺序交替插入到前半部分的节点中。所以如果 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?