I just think there's something wrong with their java answer


  • -1
    M

    I think this is the right answer

    public ListNode mergeKLists(ListNode[] lists) {
            if(lists.length==0){
    			return null;
    		}
            PriorityQueue<ListNode> pQueue= new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
                @Override
                public int compare(ListNode o1,ListNode o2){
                   return o1.val-o2.val;
                }
            });
            
    	for (ListNode listNode : lists) {
    		if(listNode!=null){
    			getqueue(listNode,pQueue);
    		}
    	}
    	ListNode lNode=new ListNode(0);
    	ListNode lNode1=lNode;
    	while (!pQueue.isEmpty()){
    		lNode1.next=pQueue.poll();
    		lNode1=lNode1.next;
    	}
    	return lNode.next;
        }
        public  PriorityQueue getqueue(ListNode ln,PriorityQueue pQueue){
    		pQueue.add(ln);
    		if(ln.next!=null){
    			getqueue(ln.next,pQueue);
                            ln.next=null;
    		}
    		return pQueue;
    	}
    
    

Log in to reply
 

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