python - 将链表节点放入 Python 优先级队列的问题
问题描述
我基本上是在尝试将链接列表节点放入优先级队列中。这里ListNode是我的 LinkedList 类,lists是一个包含不同链表头的 python 列表。我不知道为什么会收到以下错误消息。
TypeError:“ListNode”和“ListNode”的实例之间不支持“<”
Q = PriorityQueue()
for node in lists:
if node:
Q.put((node.val,node))
链表类:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
解决方案
哦,我终于明白了。实际上是因为相同的优先级。只要有两个具有相同优先级的值,优先级队列就会尝试比较ListNode对象并引发此错误。一个简单的解决方案是使用计数器变量。
Q = PriorityQueue()
count = 0
for node in lists:
if node:
Q.put((node.val, count, node))
count += 1
推荐阅读
- dart - 必须将谷歌访问令牌两次发布到 keycloak 以在令牌交换中检索图片和语言环境
- c# - MonoTorrent C# 下载种子
- python - 我正在学习 Flask,我正面临这个问题
- php - 使用 curl 从 webservice bt 加载数据出错
- julia - 如何检查 Flux.jl 是否正在使用 GPU?
- kubernetes - 通过 argo-events 的传感器创建作业时,我遇到了权限问题(无法在 API 组“批处理”中创建资源“作业”)
- python - 余弦距离大于 1
- javascript - TypeError:无法读取 setTimeout() 中未定义的属性“then”
- javascript - 三.js中的OBJLoader显示黑屏
- vba - 如何在Excel中检查单元格值是否为中文文本