Merge K sorted Linked list | Linked List | Important |Love Babbar DSA Sheet | Amazon | Google🔥

Sdílet
Vložit
  • čas přidán 16. 04. 2021
  • #Linkedlist #competitiveprogramming #coding #dsa
    Hey, Guys in this video I have explained with code how we can solve the problem 'Merge K sorted Linked list '.
    Join our Telegram Channel for more Information
    🔰 Telegram Channel Link = t.me/CodeLibrary1
    🔰 Get 10% OFF on all GeeksforGeeks Courses
    Coupon Code = CODELIBRARY
    🔰 Array Playlist = • Love Babbar DSA 450 Qu...
    🔰 String Playlist = • Love Babbar DSA 450 Qu...
    🔰 Searching and Sorting Playlist = • Love Babbar DSA 450 Qu...
    🔰 Binary Tree Playlist = • Love Babbar DSA 450 Qu...
    🔰 Linked List Playlist = • Love Babbar DSA 450 Qu...
    🔰 Graph Playlist = • Love Babbar DSA 450 Qu...
    🔰 Dynamic Programming Playlist = • Love Babbar DSA 450 Qu...
    🔰 Informative Videos = • Informative Videos
    🔰 Love Babbar DSA Sheet: drive.google.com/file/d/1FMdN...
    Follow us on Instagram:
    🔰 Shailesh Yogendra : / shaileshyogendra
    🔰 Yogesh Yogendra : / i_am_yogesh_here
    Follow us on LinkedIn:
    🔰 Yogesh Yogendra : / yogesh-yogendra-26bbb518a
    🔰 Shailesh Yogendra : / shailesh-yogendra-8b13...
    Hope you like it. Comment if you have any doubt
    LIKE | SHARE | SUBSCRIBE

Komentáře • 29

  • @gigachad6844
    @gigachad6844 Před 2 lety +5

    Best explanation so far, I've seen people using Priority Queues, Heaps and making it unnecessarily hard, why just now keep merging 2 lists until we're left with one only (answer). Thanks man

  • @mickyman753
    @mickyman753 Před 3 lety +4

    the explaination was amazing ,i didn't thought there can be better approach then nk

  • @anishprasad7024
    @anishprasad7024 Před 3 lety +6

    Second approach m apne Space complexity Log K batatya h but , wo K hona cahiye right because after every iteration at a time priority Queue/ Heap m at max K values reh rahe h ? Video time 8:13

    • @CodeLibrary
      @CodeLibrary  Před 3 lety +2

      ya correct it will be O(K) only........by mistake i told O(Log k)

  • @chitej7373
    @chitej7373 Před 3 lety +1

    Bro thanks for the great work and consistency

  • @himanshunagar1510
    @himanshunagar1510 Před 2 lety +1

    Thank You Bro , Nice Explanation

  • @snehabaser3155
    @snehabaser3155 Před 3 lety +1

    Thanks man!

  • @arushimathur7205
    @arushimathur7205 Před 2 lety

    Great Explanation !!

  • @bhushanmahajan9107
    @bhushanmahajan9107 Před 3 lety +2

    thanks man

  • @sayantaniguha8519
    @sayantaniguha8519 Před 2 lety

    Can someone write the recurrence relation for the divide & conquer approach ?

  • @1piecegaming622
    @1piecegaming622 Před 3 lety +3

    next video on clone a linked list please

  • @it17harshahuja81
    @it17harshahuja81 Před 11 měsíci +1

    bro no need for i++; only j--; is sufficient because on dry run it is taking same number of steps 👍👍👍👍

  • @arnabroy2995
    @arnabroy2995 Před rokem

    Very nice explanation ❤

  • @raviashwin1157
    @raviashwin1157 Před 3 lety

    clear explanation and clear code ❤️

  • @lokanathpatasahani897
    @lokanathpatasahani897 Před 2 lety +2

    Bro for after merging the linked lists you have to sort as you are not using priority queue in the last approach so it will take some extra time for sorting.
    So I think in the last approach the overall time complexity will be O(nlogn).

  • @HimanshuYadav-yj4to
    @HimanshuYadav-yj4to Před 3 lety +8

    In the last approach, I think recursive call for smerge() function will also consume space in memory stack. So, I don't feel it's a O(1) space solution... Btw good explanation!

    • @arnabdutta4662
      @arnabdutta4662 Před rokem +1

      Same bro

    • @tarunkumar.d8379
      @tarunkumar.d8379 Před rokem

      Nope , we are not making recursive call with a whole array or any data structure but just a pointer that points to a address, so it's negligible.

  • @sayantaniguha8519
    @sayantaniguha8519 Před 2 lety +1

    8:21 - Divide & Conquer Approach

  • @harshyallewar5876
    @harshyallewar5876 Před 2 lety

    Nice bro

  • @palashagrawal2343
    @palashagrawal2343 Před 3 lety +2

    are u solving every questions of babar?

  • @rahulrggupta27
    @rahulrggupta27 Před 3 lety

    two main approaches 1. heap (PQ), 2. 12.59s

  • @yashsolanki2357
    @yashsolanki2357 Před 2 lety +1

    can anyone explain me what is " linked lost ko dhaal do " 😂😂😂😂

  • @kritekagrawal7340
    @kritekagrawal7340 Před 2 lety

    java Program
    for(int i=0;i

  • @harshidatanku6561
    @harshidatanku6561 Před 2 lety

    Give the code in github

  • @Riya-ly6uw
    @Riya-ly6uw Před rokem +1

    very bad

  • @arnabdutta4662
    @arnabdutta4662 Před rokem +1

    Most optimized JAVA solution -
    public ListNode mergeKLists(ListNode[] lists) {

    int k = lists.length;
    if(k == 0) return null;
    if(k == 1) return lists[0];

    // int i = 0;
    int last = k-1;
    // int j = last;
    while(last != 0){
    int i = 0;
    int j = last;

    while(i < j){
    lists[i] = merge(lists[i],lists[j]);
    i++;
    j--;

    if(i >= j){
    last = j;
    }
    }

    }

    return lists[0];
    }

    ListNode merge(ListNode head1 , ListNode head2){

    if(head1 == null)return head2;
    if(head2 == null)return head1;

    ListNode dummy = new ListNode(0);
    ListNode temp = dummy;

    while(head1 != null && head2 != null){

    if(head1.val < head2.val){
    temp.next = head1;
    temp = temp.next;
    head1 = head1.next;
    }
    else{
    temp.next = head2;
    temp = temp.next;
    head2 = head2.next;
    }
    }

    if(head1 != null)temp.next = head1;
    if(head2 != null)temp.next = head2;

    return dummy.next;
    }
    Hope it helps :)