0
votes

I'm basically trying to put Linked list nodes into a priority queue. Here ListNode is my LinkedList class and lists is a python list that contains heads of different linked lists. I've no idea why I'm getting the following error message.

TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'

  Q = PriorityQueue()
for node in lists:
    if node:
        Q.put((node.val,node))

LinkedList class:

class ListNode:
def __init__(self, val=0, next=None):
    self.val = val
    self.next = next
1

1 Answers

0
votes

oh, I finally get it. It's because of the same priority actually. Whenever there are two values with the same priority, Priority queue tries to compare the ListNode object and raise this error. An easy solution is to use a counter variable.

Q = PriorityQueue()
count = 0
for node in lists:
   if node:
       Q.put((node.val, count, node))
       count += 1