python 合并k个有序链表

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from heapq import heappush, heappop
 
 
class Solution:
    def mergeKLists(self, lists):
        q = []
        for i,head in enumerate(lists):
            if head:
                heappush(q, (head.val, i, head))
 
        node = dummy = ListNode(0)
        while q:
            val, i, pop_node = heappop(q)
            print(val)
            node.next = ListNode(val)
            node = node.next
            next_node = pop_node.next
            if next_node:
                heappush(q, (next_node.val, i, next_node))
        return dummy.next

  

 为啥有i???????????理由见后、

 

另外PQ也有这个问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
import queue
 
 
class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        q = queue.PriorityQueue()
        for i,head in enumerate(lists):
            if head:
                q.put((head.val, i, head))
         
        node = dummy = ListNode(0)
        while not q.empty():
            val, i, pop_node = q.get()
            node.next = ListNode(val)
            node = node.next
            next_node = pop_node.next
            if next_node:
                q.put((next_node.val, i, next_node))
        return dummy.next

  

python 3 heappush Line 13: TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'

出现原因:

@cbmbbz In the event that two or more of the lists have the same val, this code will error out since the queue module will compare the second element in the priority queue which is a ListNode object (and this is not a comparable type).

 

To solve for this issue, I have stored (node.val, list_idx, node) to account for this edge case.

 

from queue import PriorityQueue
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeKLists(self, lists):
        k = len(lists)
        q = PriorityQueue(maxsize=k)
        dummy = ListNode(None)
        curr = dummy
        for list_idx, node in enumerate(lists):
            if node: q.put((node.val, list_idx, node))
        while q.qsize() > 0:
            poped = q.get()
            curr.next, list_idx = poped[2], poped[1]
            curr = curr.next
            if curr.next: q.put((curr.next.val, list_idx, curr.next))
        return dummy.next

 

 
 
48
 
Show 4 replies
Reply
Share
Report
Read More

Python3 env will get TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'
if there are duplicate values in the list. Works in Python env.

posted @   bonelee  阅读(1605)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2018-02-28 leetcode 617. Merge Two Binary Trees
2018-02-28 tflearn alexnet iter 10
2018-02-28 Attention机制讲解——其实本质思想很简单,给你一句话I am a king. 让你生成下面一句话,你第一眼想到的power,至高无上,高贵这些词,这些就是高注意力(权重)单词,而attension就是学习到这些权重而已
2018-02-28 深度学习的seq2seq模型——本质是LSTM,训练过程是使得所有样本的p(y1,...,yT‘|x1,...,xT)概率之和最大
2018-02-28 java 提取主域名
2017-02-28 elasticsearch function_score Query——文档排序结果的最后一道墙
2017-02-28 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复
点击右上角即可分享
微信分享提示