Merge Two Sorted Linked Lists (LeetCode 21) | Full solution with Examples | Study Algorithms

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
    Merge Two Sorted Lists is a programming challenge on LeetCode. You are given two list heads or we refer to them as two linked lists. These lists are already sorted. We need to merge them and return a single sorted linked list. The new list should be made by splicing together the nodes of the first two lists. Watch the video to understand the problem in a simplified manner. I then work along with you to solve it first using a Brute Force approach, and then an efficient approach. All along with visuals and explanations.
    00:00 - Intro
    00:27 - Problem Statement and Test Case
    01:07 - Brute Force Method
    02:44 - Efficient Solution
    04:54 - Dry-run of code
    📚 Links I talk about in the video:
    Actual problem on LeetCode: leetcode.com/problems/merge-t...
    Code on Github: github.com/nikoo28/java-solut...
    Test cases on GitHub: github.com/nikoo28/java-solut...
    📘 A text based explanation is available at: studyalgorithms.com/link_list...
    Read more:
    What are linked lists: studyalgorithms.com/link_list...
    Common operations on a linked list: studyalgorithms.com/link_list...
    To see more videos like this, you can show your support on www.buymeacoffee.com/studyalg...
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    #leetcode #programming #tutorial

Komentáře • 50

  • @ashok2089
    @ashok2089 Před 5 měsíci +2

    You made it easily understandable with the help of clearly drawing example...Thanks!

  • @souravdebnathx
    @souravdebnathx Před 3 lety +9

    best easy explaination ever i searched!

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

      Glad I could help you out :)

  • @SOURAV-tu5jw
    @SOURAV-tu5jw Před 3 lety +1

    Thanks. Keep doing the good work

  • @gokulnaathb2627
    @gokulnaathb2627 Před 5 měsíci

    Super approach!

  • @satyamgupta6030
    @satyamgupta6030 Před rokem +2

    great explaination bhaiya keep making such awesome videos for us.

    • @nikoo28
      @nikoo28  Před rokem +1

      so glad you liked it

  • @somsaichowdhary861
    @somsaichowdhary861 Před rokem

    great explaination bhaiya

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 Před 10 měsíci +1

    NICE SUPER EXCELLENT MOTIVATED

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

    thanks brother!!

  • @ekacaesium4043
    @ekacaesium4043 Před 5 měsíci

    this was the best man .....

  • @zewdih3871
    @zewdih3871 Před 8 měsíci

    Excellent Explanation

    • @nikoo28
      @nikoo28  Před 7 měsíci

      Glad it was helpful!

  • @everyontech2716
    @everyontech2716 Před rokem

    great job

  • @robinsingh202
    @robinsingh202 Před rokem +1

    NIcely Done!

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

    Hi, thank you so much for this explanation. Why the variable headNode never used?

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

      We need a starting point when creating a new list. This headnode acts as a sentinel node which is used for temporary purpose and such that we don’t lose track of the starting point.

    • @zack176
      @zack176 Před rokem

      ​@@nikoo28is this python language

    • @priyanshgarg1292
      @priyanshgarg1292 Před rokem

      no java@@zack176

  • @subee128
    @subee128 Před 7 měsíci +1

    Thanks

  • @taniyasharma8924
    @taniyasharma8924 Před 4 dny

    Hey, this is giving time limit exceeded on leetcode.. can you please help

  • @sarthakgupta096
    @sarthakgupta096 Před 5 měsíci

    Bhaiya thanku 😊

  • @preetijavali6595
    @preetijavali6595 Před 4 měsíci

    Hi Thanks again for the video. The part where appending remaining list if any one ofthem reach to null, doesn't that code become unreachable?

    • @nikoo28
      @nikoo28  Před 4 měsíci

      no, because you are running the first loop only until one of the list reaches null. So it could be possible that l2 reaches null first, while not l1
      Then you will jump at the lower portion of code.

  • @midhuns2934
    @midhuns2934 Před 3 lety

    Why are we assigning the dummy node to another node??

    • @nikoo28
      @nikoo28  Před 3 lety

      We need a starting point for our result. This dummy node serves the purpose.

  • @user-ym8wk9yv3y
    @user-ym8wk9yv3y Před 10 měsíci +1

    sir but we are creating a new list doesn't it consumes extra space

    • @nikoo28
      @nikoo28  Před 10 měsíci +1

      we are not creating a new list, we are just assigning pointers. This is the beauty of a linked list. :) Watch my theoretical videos on linked lists for a better understanding.

  • @rakshan4253
    @rakshan4253 Před 7 měsíci

    Just my doubt if anyone can clear will be really helpful , why headNode is never used but at last headNode is returned?
    it will be having the sorted list??

    • @nikoo28
      @nikoo28  Před 7 měsíci

      Because we are playing with pointers. Watch my introductory videos on linked lists for a better understanding :)

  • @vaishnevprabu6474
    @vaishnevprabu6474 Před 2 měsíci

    It results in a runtime error

  • @ashok2089
    @ashok2089 Před 16 dny

    Nikhil, Can you please do a video on Merge K Sorted List?

    • @nikoo28
      @nikoo28  Před 15 dny

      Yes, I plan to do it, it will be very similar to this approach though

    • @ashok2089
      @ashok2089 Před 14 dny

      @@nikoo28 Thank you!

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

    returnNode = returnNode.next code line is not explained. Can you please.

    • @nikoo28
      @nikoo28  Před 3 lety

      That is because the initial node we created was a temporary node. So that we have something to start building.
      The actual merged list starts from the next node.
      Hence we return the next.

    • @swetaswarnakar828
      @swetaswarnakar828 Před 2 lety

      @@nikoo28 how is it constant space? are not we storing them in returnNode? so it should be O(n + m) space and time also???

  • @user-kv2gd5nv1u
    @user-kv2gd5nv1u Před rokem +1

    Sir, could you please do lectures on Python data structures

    • @nikoo28
      @nikoo28  Před rokem +1

      what data structures are you looking for?

    • @user-xx6dv1tz2j
      @user-xx6dv1tz2j Před rokem

      @@nikoo28 stacks,queues,linked lists,hash maps,collection frameworks

  • @gurunathkulkarni6397
    @gurunathkulkarni6397 Před rokem

    Sir, it is saying time limit exceeded

    • @aashushaikh5843
      @aashushaikh5843 Před rokem

      yes and also it s giving :
      java.lang.NullPointerException: Cannot read field "val" because "" is null
      this error can u tell. me why?:

    • @nikoo28
      @nikoo28  Před rokem

      did you check out the code on github given in the video description?

  • @trader184
    @trader184 Před rokem

    What is the last line tell me🤧🤧🤧🤧🤬

  • @anshukumarchoudhary3833

    man why have u made this video when u did not explain each line.