Merge Two Sorted Linked Lists (LeetCode 21) | Full solution with Examples | Study Algorithms
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
You made it easily understandable with the help of clearly drawing example...Thanks!
best easy explaination ever i searched!
Glad I could help you out :)
Thanks. Keep doing the good work
Super approach!
great explaination bhaiya keep making such awesome videos for us.
so glad you liked it
great explaination bhaiya
NICE SUPER EXCELLENT MOTIVATED
thanks brother!!
this was the best man .....
Excellent Explanation
Glad it was helpful!
great job
NIcely Done!
Hi, thank you so much for this explanation. Why the variable headNode never used?
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.
@@nikoo28is this python language
no java@@zack176
Thanks
Hey, this is giving time limit exceeded on leetcode.. can you please help
Bhaiya thanku 😊
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?
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.
Why are we assigning the dummy node to another node??
We need a starting point for our result. This dummy node serves the purpose.
sir but we are creating a new list doesn't it consumes extra space
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.
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??
Because we are playing with pointers. Watch my introductory videos on linked lists for a better understanding :)
It results in a runtime error
Nikhil, Can you please do a video on Merge K Sorted List?
Yes, I plan to do it, it will be very similar to this approach though
@@nikoo28 Thank you!
returnNode = returnNode.next code line is not explained. Can you please.
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.
@@nikoo28 how is it constant space? are not we storing them in returnNode? so it should be O(n + m) space and time also???
Sir, could you please do lectures on Python data structures
what data structures are you looking for?
@@nikoo28 stacks,queues,linked lists,hash maps,collection frameworks
Sir, it is saying time limit exceeded
yes and also it s giving :
java.lang.NullPointerException: Cannot read field "val" because "" is null
this error can u tell. me why?:
did you check out the code on github given in the video description?
What is the last line tell me🤧🤧🤧🤧🤬
man why have u made this video when u did not explain each line.
Which part are you facing a problem at? I can help you out further.
@@nikoo2805 last line
chup reh
we understood bro