Merge k Sorted Lists

Source: Internet
Author: User

Question

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Method
Use the thought of merging and sorting to merge the data until it finally becomes one.
    public ListNode mergeKLists(ArrayList
  
    lists) {        int len = lists.size();        if(len == 0){            return null;        }        int n = ( len + 1 ) / 2;        while(len > 1){            for(int i = 0; i < n ; i++){                if(n + i < len){                    if(lists.get(i) == null){                        lists.set(i, lists.get(n + i));                        lists.remove(n + i);                    }else if(lists.get(n + i) == null){                        lists.remove(n + i);                    }else{                        ListNode first = lists.get(i);                        ListNode second = lists.get(n + i);                        ListNode head = null;                        ListNode currentNode = null;                        ListNode node;                        while(first != null && second != null){                            if(first.val < second.val){                                node = first;                                first = first.next;                            }else{                                node = second;                                second = second.next;                            }                            if(head == null){                                head = node;                                currentNode = head;                            }else{                                currentNode.next = node;                                node.next = null;                                currentNode = node;                            }                        }                        if(first != null){                            currentNode.next = first;                        }else{                            currentNode.next = second;                        }                         lists.set(i, head);                        lists.remove(n + i);                                            }                }                len = lists.size();                n = (len + 1) / 2;            }        }        return lists.get(0);    }
  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.