Simple Java


  • 0
    J

    public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
    if(lists.length == 0) return null;
    ListNode res = lists[0];

        int hi = lists.length-1;
        
        while(hi != 0){
            int lo = 0;
            while(lo < hi){
                lists[lo] = merge(lists[lo], lists[hi]);
                lo++;
                hi--;
            }
        }
        return lists[0];
    }
    
    private ListNode merge(ListNode s1, ListNode s2){
        ListNode k;
        if(s1 == null){
            return s2;
        }
        if(s2 == null){
            return s1;
        }
        
        if(s1.val > s2.val){
            k = s2;
            k.next  = merge(s1, s2.next);
        }else{
            k = s1;
            k.next  = merge(s2, s1.next);
        }
        return k;
    }
    

    }


Log in to reply
 

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