Merge Sort for Linked List | Very Important | Linked List | DSA Sheet [Explaination + CODE] Amazon🔥

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ม.ค. 2025

ความคิดเห็น • 88

  • @SanyamBanoni
    @SanyamBanoni 2 ปีที่แล้ว +11

    bro you can dry run for some input to give a proper understanding to your viewers ..
    this tutorial is good only for those who just need to revise the concept
    for someone who is first time seeing this problem this tutorial fails to teach them

  • @soniyarizvi1200
    @soniyarizvi1200 3 ปีที่แล้ว +2

    your video is really amazing , I'm dealing with linked lists from last 2 days but didn't get the concept of merge sort for linked list . your video helps me to clear my concept .

  • @srikrupa7445
    @srikrupa7445 3 ปีที่แล้ว +5

    Sir, your videos are very very helpful,I had a great trouble in getting my doubts cleared, please continue posting these videos sir

  • @prathamanand1037
    @prathamanand1037 3 ปีที่แล้ว +8

    class Solution{
    public:
    void MergeSorting(Node** head)
    {
    Node *cur = *head;
    Node *first;
    Node *second;
    if(!cur or !cur->next) return;
    FindMiddle(cur, &first, &second);
    MergeSorting(&first);
    MergeSorting(&second);
    *head = MergeBoth(first, second);
    }
    Node* MergeBoth(Node* first, Node* second)
    {
    Node* answer= NULL;
    if(!first) return second;
    else if(!second) return first;
    if(first->data data)
    {
    answer = first;
    answer -> next = MergeBoth(first->next, second);
    }
    else{
    answer = second;
    answer -> next = MergeBoth(first, second -> next);
    }
    return answer;
    }
    void FindMiddle(Node* cur, Node** first, Node **second)
    {
    Node* fast;
    Node* slow;
    slow = cur;
    fast = cur ->next;
    while(fast != NULL){
    fast = fast ->next;
    if(fast != NULL)
    {
    slow = slow ->next;
    fast = fast -> next;
    }
    }
    *first = cur;
    *second = slow -> next;
    slow -> next = NULL;
    }
    //Function to sort the given linked list using Merge Sort.
    Node* mergeSort(Node* head) {
    // your code here
    MergeSorting(&head);
    return head;
    }
    };
    class Solution{
    public:
    void MergeSorting(Node** head)
    {
    Node *cur = *head;
    Node *first;
    Node *second;
    if(!cur or !cur->next) return;
    FindMiddle(cur, &first, &second);
    MergeSorting(&first);
    MergeSorting(&second);
    *head = MergeBoth(first, second);
    }
    Node* MergeBoth(Node* first, Node* second)
    {
    Node* answer= NULL;
    if(!first) return second;
    else if(!second) return first;
    if(first->data data)
    {
    answer = first;
    answer -> next = MergeBoth(first->next, second);
    }
    else{
    answer = second;
    answer -> next = MergeBoth(first, second -> next);
    }
    return answer;
    }
    void FindMiddle(Node* cur, Node** first, Node **second)
    {
    Node* fast;
    Node* slow;
    slow = cur;
    fast = cur ->next;
    while(fast != NULL){
    fast = fast ->next;
    if(fast != NULL)
    {
    slow = slow ->next;
    fast = fast -> next;
    }
    }
    *first = cur;
    *second = slow -> next;
    slow -> next = NULL;
    }
    //Function to sort the given linked list using Merge Sort.
    Node* mergeSort(Node* head) {
    // your code here
    MergeSorting(&head);
    return head;
    }
    };
    Thank me later

  • @vanshitachoudhary9572
    @vanshitachoudhary9572 3 ปีที่แล้ว +27

    can you please make a video on quick sort for linked list

  • @PrakrutiYadavMusic
    @PrakrutiYadavMusic ปีที่แล้ว +1

    You deserve a thumbs up for creating these helpful videos 👍👍👍

  • @abhishekshankar1136
    @abhishekshankar1136 3 ปีที่แล้ว +26

    this was asked in a coding competition in my college
    so i simply dumped all the linked list content into a vector, sorted the vector and copied the value back into linked list 🤣🤣😛😛

    • @ritesh7880
      @ritesh7880 3 ปีที่แล้ว

      Lol😂

    • @adityaagrawal190
      @adityaagrawal190 3 ปีที่แล้ว +4

      but that wont work in o(1) space tho, the point is that u have to do without using any extra space :)

    • @GS7999
      @GS7999 3 ปีที่แล้ว

      Piro

    • @swapnilkant2793
      @swapnilkant2793 3 ปีที่แล้ว

      Inefficient solution

    • @ruchirrathore5055
      @ruchirrathore5055 3 ปีที่แล้ว

      @@adityaagrawal190 here also we are making new node for each node.

  • @rajuc6438
    @rajuc6438 2 ปีที่แล้ว

    Thank you. Very helpful video

  • @ManishSharma-ys5ws
    @ManishSharma-ys5ws 2 ปีที่แล้ว +2

    Why you have taken fast pointer as head->next ??? In your "mid element in linked list" video you have taken it as head only ???

  • @stith_pragya
    @stith_pragya ปีที่แล้ว

    Thank You So Much Yogesh bhaiya for this amazing content......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @dyknows4238
    @dyknows4238 3 ปีที่แล้ว +1

    bro how much time did it take u to finish love Babbar 450 questions.

  • @shriyaverma1195
    @shriyaverma1195 3 ปีที่แล้ว +6

    You are doing an amazing job. Thankyou so much for helping out in these problems

  • @namansrivastava9495
    @namansrivastava9495 3 ปีที่แล้ว +2

    Bhai Love babbar ki poori sheet bana de , sbka bhala ho jayega !!!!!
    All the best for accolite internship,macha dena !!!!

  • @MohitKumar-md9re
    @MohitKumar-md9re 3 ปีที่แล้ว +16

    I would like to recommend you to share the link for your code too for ease in understanding , this would be really helpful

    • @abhishekhorton79
      @abhishekhorton79 3 ปีที่แล้ว +4

      Try to implement by yourself bro it would be helpful just write the approach and code it

    • @SanyamBanoni
      @SanyamBanoni 2 ปีที่แล้ว

      bro understand the video and then try to implement it yourself this will help me understanding the problem more clearly
      and also for long term it is recommended to implement it yourself

  • @rakshitasharma1995
    @rakshitasharma1995 3 ปีที่แล้ว +2

    your videos are really helpful .Thanks for your help!!!

  • @revampwithastha
    @revampwithastha 3 ปีที่แล้ว +3

    I am having a segmentation fault in this code

  • @ishant162
    @ishant162 3 ปีที่แล้ว +1

    Really nice explaination. Can you make a video of quicksort for LinkedList

  • @justakidchannel6953
    @justakidchannel6953 3 ปีที่แล้ว

    it's awesome bro help me alot in my Dsa journey

  • @codeofcoffee8722
    @codeofcoffee8722 2 ปีที่แล้ว

    Which software you are using?, May I know pls .

  • @ShubhamKumar-et7gx
    @ShubhamKumar-et7gx 3 ปีที่แล้ว +1

    find middle() me curr ko single pointer aur first and second double pointer kyu passkiye?

  • @muditguraria9065
    @muditguraria9065 3 ปีที่แล้ว

    Thank you. I was struggling with this so much aaarrrggghh!!

  • @smitboraniya6752
    @smitboraniya6752 2 ปีที่แล้ว +3

    Bro, you are just reading the code. I learned nothing new here. If you want to make better videos then, explain key details like, why we are using pointer of pointer here? and Why we are using merge step recursively? can we do that as non recursive array type merge sort?

  • @ashokk2002
    @ashokk2002 3 ปีที่แล้ว +3

    Bhaiya... Why are we using Node** first .. It should be Node* na??

    • @varungoel3633
      @varungoel3633 3 ปีที่แล้ว

      pass by ref hai node, isliye

  • @pixelsbyme
    @pixelsbyme 2 ปีที่แล้ว +1

    i am getting segmentation error due to below code for finding middle element, can someone please point out the mistake?
    void FindMiddle(Node *curr, Node **first, Node **second)
    {
    if (curr == NULL)
    {
    *first = NULL;
    *second = NULL;
    return;
    }
    Node *slow = curr;
    Node *fast = curr;
    while (fast && fast->next)
    {
    slow = slow->next;
    fast = fast->next->next;
    }
    *first = curr;
    *second = slow->next;
    slow->next = NULL;
    }

  • @enigmanarratives1
    @enigmanarratives1 2 ปีที่แล้ว +1

    why no one explaining O(1) space comp solution ?

  • @zavier3436
    @zavier3436 3 ปีที่แล้ว

    Bhaiya you are amazing... your videos are really helping a lot.... I have a request ...plz put the code in ur description or in the chat box plz

  • @1piecegaming622
    @1piecegaming622 3 ปีที่แล้ว +2

    please make video on Quick sort for linked list

  • @sanchisingh5117
    @sanchisingh5117 ปีที่แล้ว

    why are we using pointer of pointer here? we didn't we just use simple pointer instead?

  • @anunay_sharma
    @anunay_sharma ปีที่แล้ว

    whose sheet is this?

  • @rasid_khan
    @rasid_khan 3 ปีที่แล้ว +1

    Where i got the code

  • @prasannapm3220
    @prasannapm3220 2 ปีที่แล้ว

    thanks!

  • @anuragtiwari3032
    @anuragtiwari3032 3 ปีที่แล้ว

    great explanation!!! and can u share the excel that u have containing all the problem links

  • @obamaengineer4806
    @obamaengineer4806 3 ปีที่แล้ว

    Bhaiya what is use of double pointer that i couldnt understand

    • @shubhamsonawane967
      @shubhamsonawane967 3 ปีที่แล้ว

      double pointer is used for storing address of another pointer

  • @md.ualiurrahmanrahat2400
    @md.ualiurrahmanrahat2400 3 ปีที่แล้ว

    Can you explain why did you use double pointer(** ) in the code? You didn't explain that and I did not get that.

    • @RajputAnkit11
      @RajputAnkit11 3 ปีที่แล้ว

      because of call by reference

  • @VISHALSHARMA-mu9kl
    @VISHALSHARMA-mu9kl 3 ปีที่แล้ว

    thankyou so much brother

  • @juhabach6371
    @juhabach6371 2 ปีที่แล้ว

    bhai..wo excel sheet upload karde..net se kisi ne hata diya...abhi pdf hai, but excel nahi hai

  • @jahnavireddy3369
    @jahnavireddy3369 3 ปีที่แล้ว

    TQSM......

  • @sohelrana1342
    @sohelrana1342 3 ปีที่แล้ว

    What about space complexity..

  • @lastminuteprogrammer8511
    @lastminuteprogrammer8511 3 ปีที่แล้ว

    bhai apne code ki link bhi share kar diya karo please...

  • @aakashsaini9970
    @aakashsaini9970 3 ปีที่แล้ว

    space complexity??

  • @AdityaSharma-ei4gq
    @AdityaSharma-ei4gq 3 ปีที่แล้ว +1

    Baki sab to accha hai bas ek graphic pad le lo

  • @codeandtalk6
    @codeandtalk6 3 ปีที่แล้ว

    Why fast is pointed to curr next why not to curr

    • @Doraemon-em4fy
      @Doraemon-em4fy 3 ปีที่แล้ว

      yess please explain thus why is pointing it to curr gives a segmentation fault

    • @RajputAnkit11
      @RajputAnkit11 3 ปีที่แล้ว

      @@Doraemon-em4fy you can do both but inboth cases condition of while loop differs

  • @sohelrana1342
    @sohelrana1342 3 ปีที่แล้ว

    As in gfg expected space complexity is o(n)

  • @abhinavagrawal6148
    @abhinavagrawal6148 3 ปีที่แล้ว +1

    Fir se daily 2 videos kb aayegi 😥😥😥

    • @CodeLibrary
      @CodeLibrary  3 ปีที่แล้ว +2

      Aj last exam tha...kal se a jaega😀

  • @M10-r8q7h
    @M10-r8q7h 3 ปีที่แล้ว +6

    Segmentation fault 😭😭😭

  • @rohit_700
    @rohit_700 3 ปีที่แล้ว +2

    bhai koi force kr rha hai kya tumhe videos banane ke liye

  • @subhamengine1143
    @subhamengine1143 3 ปีที่แล้ว

    Bro quick sort for LinkedList please!!

  • @nitishshaw6036
    @nitishshaw6036 3 ปีที่แล้ว +5

    Maha bakwaas......what is the point if you are not explaining how the pointers move and arrange while merging back again,
    Its very easy to say to read the code and telling aisa ho jaega

    • @sachinsharma905
      @sachinsharma905 3 ปีที่แล้ว

      I second you.

    • @RajputAnkit11
      @RajputAnkit11 3 ปีที่แล้ว

      this happens when you dont know how to merge two sorted linked list.
      he already made video on it

  • @achillles6838
    @achillles6838 3 ปีที่แล้ว

    Yeh sare videos konsi language me hai??

  • @B.Shouvik17
    @B.Shouvik17 3 ปีที่แล้ว

    1st view

  • @prasannapm3220
    @prasannapm3220 2 ปีที่แล้ว

    source code pls

  • @piyushsarraf9994
    @piyushsarraf9994 3 ปีที่แล้ว +2

    Bhai Jaldi ye sab khatam karke BST pe aa jao

    • @CodeLibrary
      @CodeLibrary  3 ปีที่แล้ว +3

      Yaa we will come....have patience

  • @rishabhtripathi4002
    @rishabhtripathi4002 3 ปีที่แล้ว +1

    bas tu short bolna band kar de bhai

  • @giovannigiorgiobuteverybod2660
    @giovannigiorgiobuteverybod2660 3 ปีที่แล้ว

    solo

  • @sonumehra6888
    @sonumehra6888 3 ปีที่แล้ว +1

    Bhai gfg se pura code copy kar k koi bhi samja skta hai
    Apna code kudh bhi likha kar kabhi

  • @tasneemahmad7858
    @tasneemahmad7858 2 ปีที่แล้ว

    class Solution{
    public:
    //Function to sort the given linked list using Merge Sort.
    void MergeSorting(Node**head){
    Node*cur=*head;
    Node*first;
    Node*second;
    if(!cur || !cur->next) return;
    FindMiddle(cur,&first,&second);
    MergeSorting(&first);
    MergeSorting(&second);
    *head=MergeBoth(first,second);
    }
    void FindMiddle(Node*cur,Node**first,Node**second){
    Node*slow;
    Node*fast;
    slow=cur;
    fast=cur->next;
    while(fast!=NULL){
    fast=fast->next;
    if(fast!=NULL){
    slow=slow->next;
    fast=fast->next;
    }
    }
    *first=cur;
    *second=slow->next;
    slow->next=NULL;
    }
    Node*MergeBoth(Node*first,Node*second){
    Node*answer=NULL;
    if(!first){
    return second;
    }
    else if(!second){
    return first;
    }
    if(first->datadata){
    answer=first;
    answer->next=MergeBoth(first->next,second);
    }
    else{
    answer=second;
    answer->next=MergeBoth(first,second->next);
    }
    return answer;
    }
    Node* mergeSort(Node* head) {
    // your code here
    MergeSorting(&head);
    return head;
    }
    };

  • @satyamjaiswal1222
    @satyamjaiswal1222 3 ปีที่แล้ว

    can you please make a video on quick sort for linked list