A question related to JAVA override syntax

  • 0

    Hi, I slightly modified this code

        public class Solution {
        class MyComparator<ListNode> implements Comparator<ListNode> {
    		public int compare(ListNode o1, ListNode o2) {
    			return (o1.val-o2.val);
        public ListNode mergeKLists(List<ListNode> lists) {
            if (lists==null||lists.size()==0) return null;
    	    Comparator<ListNode> comparator = new MyComparator<ListNode>();
            PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.size(),comparator);
            ListNode dummy = new ListNode(0);
            ListNode tail=dummy;
            for (ListNode node:lists)
                if (node!=null)
            while (!queue.isEmpty()){
                if (tail.next!=null)
            return dummy.next;

    This leads to compile error where the compiler cannot recognize o1.val.
    When I replace "MyComparator< ListNode >" by "MyComparator", the code is accepted. Can anyone tell me why the first version leads to compile error? Thanks!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.