题解 | #奶牛排队#

奶牛排队

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

全部评论

相关推荐

程序员小白条:学历GG,这个排版布局,还有行间距和字缩进不大行,女生自我要求应该更高才是,没内容,起码美观这块要做好
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务