考察对链表的操作
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
count=0
p=head
res=[]
while p:
res.append(p)
count+=1
p=p.next
count=count>>1
p=head
while count>0:
tail=res.pop()
r=p.next
p.next=tail
tail.next=r
p=r
count-=1
p.next=None

本文介绍了一种链表操作的高级技巧——使用双指针进行链表的重构。通过实例演示了如何在不返回任何值的情况下,直接修改链表节点的连接方式,实现链表的重新排序。此方法利用了计数和尾部节点插入的策略,有效地完成了链表的前半部分和后半部分的交错连接。
616

被折叠的 条评论
为什么被折叠?



