This is the ListNode class:
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
I am trying to initialize a PriorityQueue like this:
PriorityQueue<ListNode> heap = new PriorityQueue(lists.length,
(l1, l2) -> l1.val < l2.val ? -1 :
l1.val == l2.val ? 0 :
1);
But I am getting "cannot find symbol: variable val". What is the proper way to do this? I've tried casting l1 and l2 as ListNode's, but this does not do anything.
Edit: Why does this work?
PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.size(),new Comparator<ListNode>(){
@Override
public int compare(ListNode o1,ListNode o2){
if (o1.val<o2.val)
return -1;
else if (o1.val==o2.val)
return 0;
else
return 1;
}
});