题解 | #合并k个已排序的链表#

合并k个已排序的链表

http://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6

首先是将 所有的链表的值加入到 列表中 然后进行排序 然后使用 固定头指针法则 生成一个循环体链表 返回头节点 记得处理边界问题: alt

    def mergeKLists(self , lists: List[ListNode]) -> ListNode:
        # write code here
        temp=[]
        for head in lists:
             while head:
                    temp.append(head.val)
                    head=head.next
        if len(temp)==0:
            return None
        temp.sort()
        data=pre=ListNode(0)
        for index ,i in enumerate(temp):
            pre.val=i
            if index!=len(temp)-1:# 处理边界问题
                pre.next=ListNode(0)
                pre=pre.next
        return data
                
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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