Swap Nodes in Pairs (LeetCode 24) | Full solution with animations diagrams | Study Algorithms
ฝัง
- เผยแพร่เมื่อ 4 ก.ค. 2024
- Actual Problem: leetcode.com/problems/swap-no...
Chapters:
00:00 - Intro
00:46 - Problem Statement and Description
02:56 - Gotchas to be careful of
04:54 - Building an efficient solution
10:51 - Dry-run of Code
13:42 - Final Thoughts
📚 Links to topics I talk about in the video:
Linked Lists: • Linked List Data Struc...
Traversing a Linked List: • Traversing a Linked Li...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
I love this guy's way of explanation!
I watched some videos but didn't understand them. But now, after watching your video, I finally get it. I can use this concept in another problem also. Thanks a lot for your clear explanations, bro!
Thank you! I like the way high lighting the explanation.
This is too good! thank you!
This man should be a coveted treasure. Those dedication towards drawings and details, much appreciate it my man 🫡
i loved explanation👌👌
bro your explation is too goood keep it up
clean and neat explanation
😄
Thanks you very much this was the best solution so far I watched
very good explanation , keep it up
Well explained 💯
explanation was amazing
Great explanation thanks 👍👍
damnnnnn this channnel is just unbelievably good
amazing explain 👏
too good explanation
Your teaching and problem solving skills are invaluable. Please upload more videos
New video every week
Pretty challenging to me at the moment, the one line I couldn't come up with on my own is having a dummy node & setting a helper pointer for it, doesn't seem intuitive to me but thank you for the helpful explanation on this problem! definitely need to practice more
a dummy node is very helpful, when you want to preserve the head while manipulating your linked list
easiest explanation🙂🙂
Thank you.
Sir, i got a little dizzy with this problem,i love it hahahah
How to find pattern of problems .... 🤔
Sometimes uploaded video's guided for coder , One In One Podcasts , Explain Leetcode platform , etc
Very helpful and thanks for your explanation that's awesome . . .
Happy Coding . ... . .. . . .
I appreciate your explanation, you help me to make intuition to solve this using recursion. public static Node revInPair(Node head) {
if(head == null || head.next == null) {
return head;
}
Node firstNode = head;
Node secondNode = head.next;
firstNode.next = revInPair(secondNode.next);
secondNode.next = firstNode;
return secondNode;
}
I like the line “ if you start with head some where along you will loose your head”😅
Bhaiya interview me web project ke liye kis type ke project se kaam chl sakta h or jada problem na ho.
1. Complete mern stack project.
2. Any simple react project
3. Simple With html,css or javascript.
4. Any dsa project
5. Or any dbms.
Inme se konse rakha acha rhega or sirf kitno se kaam ho jayega pls reply.
If you know the problem start at 5:00 directly.
I know the solution is correct but I cant seem to understand the last part. Since the dummy.next still points to the original (1) head node, shouldn't the returned list be 1->4->3? At what point does the dummy.next point to the 2 node?
notice that "point" points at dummy node. In the first iteration of loop we do point.next=swap2. That is where the change happens. hope that helps.
But every time while loops runs we are changing the point.next = swaps2, how it will hold the dummy.next@@nikoo28
Hi @Nikhil, thanks a lot for making these kind of videos.Helps a lot!
I want to understand why you did "point.next = swap2" assignement after swapping swap1 and swap2?
What is the reason in assigning point's next to swap 2 again as we already have dummy pointer pointing to the head of the LL?
Thanks!
exactly same question bro... that dummy node is going over my head
@sushantsabnis5835 the dummy pointer is pointing to the original head. When swapping things, the head of the linked list can change. We need to account for that scenario. Carefully see the video again at point 9:10
@shivanshsharma4916 dummy nodes just help you. Think of them as disposable nodes. Usually you would have felt that while solving problems on linked lists, there is a fear of losing head. The dummy node helps.
@@nikoo28 but you are doing point = swap1 in the next line, shouldn't that line itself take care of this?
Can we use the Concept of that first finding the mid and after getting that mid we can reverse that 2 nodes and then Again Joining them to get the Desire result ..
// 1--> 2-->3-->4
After reverse and getting the mid value
step 2 : // 2-->1 --> null
step 3: // 4---> 3 --> null
final step : - 2-->1-->4-->3--> null
Think about it...will it work for a list of length 10?
can you make videos how to approach a linklist problem like u discuss in end about the 2 approches like concept of slow ,fast and dummy node concept; some more such approches can you tell in or reply me in comment section or try to make videos on this;;
thanks a lotss ,such a nice explaination✨✨
if you check my playlist on linked lists, I do go over these approaches. There is no set guide to solve these problems, but I do try to cover a variety. In my videos I do give out hints, how you can identify a pattern.
Hope this helps!! :)
@@nikoo28 thankuu😊
i love you please please please way of explanation mat change karna.. itna purely koi aur youtube channel nahi sikha raha .. aap itna mast animation ke sath bohot acche se visulaization de rahe ho.. please agge aise video mat banane lagna ki assume this that ye wo.. keep the good work doing I love u
will try my best.
Cant get how in the end dummy->next will point to the new head of the list
If any one.................
because dummy was created in the very beginning. The next was assigned to the head of the list
Should write the code on your own, this is like cramming up the code
took way too long to explain bro can be much much faster had to watch at 2X otherwise good explanation
everyone has their own learning pace my friend :)
for quick learners, I always add timestamps for sections, and there is always an option to increase video speed :)
@@nikoo28 😄yeah bro! you are right some times some peoples don't appreciate , you did great in this video i understood thank you . I wanna speak English like you i like it
kam se kam shi code to diya kro
why do you feel the code is wrong? What test case fails for you?
@@nikoo28 it fails on all test cases
As it does not change the original linked list
@@I_W23that cannot be possible. Check the code in the video description on my github profile. It passes perfectly on LeetCode
in the code i think that in each iteration creating new swap1 and swap2 pointers would be memory exhaustive so there should be 2 pointers declared in the start only and reusing them again
@@nikoo28
Garbage collector will take care of it. Declaring new won’t exhaust the memory.
Sorry for getting back late