LeetCode 23.合并k个升序链表

 class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ArrayList<Integer> list=new ArrayList<>();
        for(int i=0;i<lists.length;i++)
        {
            while(lists[i]!=null)
            {
                list.add(lists[i].val);
                lists[i]=lists[i].next;
            }
        }
        Object [] l=new Integer[list.size()];
        l=list.toArray();
        Arrays.sort(l);
        if(l.length==0)
            return null;
        ListNode head=new ListNode((int)l[0]);
        ListNode other=head;
        for(int i=1;i<l.length;i++)
        {
            ListNode t=new ListNode((int)l[i]);
            other.next=t;
            other=other.next;
        }
        return head;
    }
}

你可能感兴趣的:(LeetCode,leetcode)