【发布时间】:2020-12-18 03:27:19
【问题描述】:
我现在正在处理重新排列链接列表的问题。代码的逻辑感觉是对的,但是遇到重复链表的无穷无尽的点。我不知道为什么,但感觉更可能是链表的定义有问题。问题是:
给定一个单链表的头部,写一个方法来修改 LinkedList 使得来自 LinkedList 后半部分的节点 从前半部分反向交替插入节点 命令。所以如果 LinkedList 有节点 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null,你的方法应该返回 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null。
您的算法不应使用任何额外的空间和输入 LinkedList 应该就地修改。
示例 1:
输入:2 -> 4 -> 6 -> 8 -> 10 -> 12 -> null 输出:2 -> 12 -> 4 -> 10 -> 6 -> 8 -> 空
示例 2:
输入:2 -> 4 -> 6 -> 8 -> 10 -> null 输出:2 -> 10 -> 4 -> 8 -> 6 -> 空
我的做法是:
def rearrange_linkedlist(head):
# find the middle as the separating point
middle = middle_linkedlist(head)
# reverse the second part
second_head = reverse_linkedlist(middle)
while head and second_head:
following_1 = head.next
following_2 = second_head.next
head.next = second_head
second_head.next = following_1
head = following_1
second_head = following_2
def middle_linkedlist(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def reserve_linkedlist(head):
current = head
previous = None
while current:
following = current.next
current.next = previous
previous = current
current = following
return previous
链表的设置:
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:
print(str(temp.value) + ' ', end='')
temp = temp.next
print()
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)
rearrange_linkedlist(head)
head.print_list()
main()
结果是:
2 12 4 10 6 8 8 8
重复 8。我不确定它有什么问题。提前感谢您的帮助。
【问题讨论】:
-
每当更新一对链接时,添加一个断言以确保它们不是同一个链接。
-
我不确定这是否是问题所在。在代码的交错部分期间,前半部分的最后一项仍指向后半部分。当它指向中间时,您已到达列表前面的末尾。
-
谢谢,@RaymondHettinger。这些问题要求空间复杂度为 O(1),所以我更新了链表。您介意解释一下关于断言的更多信息吗?
-
谢谢,@FrankYellin。我认为你很接近。由于我将链表反转到位,因此两个链表指向相同的尾部,然后指向无。但是我不确定为什么第一个链表会变得无穷无尽,或者没有结尾。请分享更多见解。
标签: python linked-list