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

合并k个已排序的链表

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

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param lists ListNode类一维数组 
# @return ListNode类
#
class Solution:
    def mergeKLists(self , lists: List[ListNode]) -> ListNode:
        # write code here
        cur_nodes = [node for node in lists if node != None]
        cur_val = [node.val for node in cur_nodes]
        res = ListNode(0)
        cur = res
        while cur_nodes != []:
            min_val = min(cur_val)
            index = cur_val.index(min_val)
            cur.next = cur_nodes[index]
            cur = cur.next
            if cur_nodes[index].next != None:
                cur_nodes[index] = cur_nodes[index].next
                cur_val[index] = cur_nodes[index].val
            else:
                cur_nodes.pop(index)
                cur_val.pop(index)
        return res.next

全部评论

相关推荐

点赞 评论 收藏
分享
05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务