# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head :
return head
pre =None
cur =head
while cur :
temp =cur.next
cur.next=pre
pre =cur
cur =temp
return pre
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
slow =head
fast =head.next
while fast and fast.next :
slow =slow.next
fast = fast.next.next
second = self.reverseList(slow.next)
slow.next =None
first =head
while second :
first_next= first.next
second_next =second.next
first.next =second
second.next=first_next
first=first_next
second=second_next
143. 重排链表
最新推荐文章于 2025-07-15 09:23:09 发布