题解 | #奶牛排队#
奶牛排队
https://www.nowcoder.com/practice/e89a2676a740471cbd4d62d4eba303ad
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def reorderCows(self , head: ListNode) -> ListNode: # write code here if not head or not head.next: return head tail = head current = head next_node = current.next if next_node: while next_node.next: current = next_node while current.next.next: current = current.next tail_next = current.next current.next = None tail.next = tail_next tail_next.next = next_node tail = next_node next_node = next_node.next if not next_node: break return head