Code Monkey home page Code Monkey logo

merge-k-sorted-lists's Introduction

Merge k Sorted Linked Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input: [ 1->4->5, 1->3->4, 2->6 ]

Output: 1->1->2->3->4->4->5->6

Implementation 1 : Naive

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length  == 0)
            return null;
        if(lists.length == 1)
            return lists[0];
        
        ListNode mergedList = mergeTwoLists(lists[0], lists[1]);
        for(int i = 2; i < lists.length; i++) {
             mergedList = mergeTwoLists(mergedList, lists[i]);   
        }
        return mergedList;
    }
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null)
            return (l1 == null) ? l2 : l1;
        ListNode p1 = l1;
        ListNode p2 = l2;
        
        ListNode head = null;
        ListNode node = null;
        ListNode current = null;
        
        while(p1 != null && p2 != null) {
            if(p1.val <= p2.val) {
                node = new ListNode(p1.val);
                p1 = p1.next;
            } else {
                node = new ListNode(p2.val);
                p2 = p2.next;
            }    
            
            if(head == null) {
                head = node;
                current = node;
            } else {
                current.next = node;
                current = node;
            }
        }
        
        while(p1 != null) {
            node = new ListNode(p1.val);
            current.next = node;
            current = node;
            p1 = p1.next;
        }
        while(p2 != null) {
            node = new ListNode(p2.val);
            current.next = node;
            current = node;
            p2 = p2.next;
        }
        return head;
        
    }
}

Implementation 2 :

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null)
            return null;
        Queue<Integer> pq = new PriorityQueue<>();
        for(ListNode node : lists){
            while(node != null){
                pq.add(node.val);
                node = node.next;
            }
        }
        ListNode head = null;
        ListNode prev = null;
        while(!pq.isEmpty()){
            ListNode node = new ListNode(pq.remove());
            if(head == null)
                head = node;
            if(prev != null)
                prev.next = node;
            prev = node;
        }
        return head;
    }
}

References :

https://www.youtube.com/watch?v=zLcNwcR6yO4

merge-k-sorted-lists's People

Contributors

emahtab avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.