Help! Works on my IDE but not AC (Runtime Error)


  • 0
    W
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            boolean flag=false;
            for(int i=0;i<lists.length;i++){
                flag=flag||(lists[i]!=null);
            }
            if(flag==false)
                return null;
            ListNode result=new ListNode(0);
            ListNode temp=result;
            ListNode[] heads=new ListNode[lists.length];
            for(int i=0;i<lists.length;i++){
                heads[i]=lists[i];
            }
            
            int min=0;
            while(flag)
            {
                for(int i=0;i<lists.length;i++){
                    if(heads[i]!=null){
                        min=i;
                        break;
                    }
                }
                for(int i=0;i<lists.length;i++){
                    if(heads[i]!=null){
                        if(heads[min].val>heads[i].val){
                            min=i;
                        }
                    }
                }
                result.next=heads[min];
                heads[min]=heads[min].next;
                result=result.next;
                flag=false;
                for(int i=0;i<heads.length;i++){
                    flag=flag||(heads[i]!=null);
                }
            }
            result.next=null;
            return temp.next;
        }
    }
    

  • 0
    E

    Having the same problem as yours...


Log in to reply
 

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