Python: Using tuple in heap is a problem

I am using python to code for this concept pack.
In this particular module, I have passed all the test cases but after completion when I saw the approach provided, I think it is not possible in python
I am using heapq for this. As provided in the solution we can add the node in the heap one by one but in python it won’t compare the nodes. I have tried a similar approach with PriorityQueue and it results with the same TypeError.
my code which generates TypeError:-


where l is the heap array

TypeError: ‘<’ not supported between instances of ListNode and ListNode

I know why it is generating that, my question is how else, in python, can we do this question with this approach?

In python, when you use a tuple in a heap , the heap first compares the first element of the 2 tuples, if equal then compare the second element and so on.

In this case your first element is node.val (which is integer and is comparable) but the second element is a node object and there is parameter to compare them. A way to solve this is to make a wrapper class to your node which compares 2 nodes by their value using the __lt__(self,other) function for the class.

Refer to this or similiar stack overflow article for better reference.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.