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

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

  • @bradleyli1569
    @bradleyli1569 11 หลายเดือนก่อน +7

    I love this guy's way of explanation!

  • @ratneshkumarsingh1454
    @ratneshkumarsingh1454 2 หลายเดือนก่อน +2

    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!

  • @hal-mr3jw
    @hal-mr3jw 4 หลายเดือนก่อน

    Thank you! I like the way high lighting the explanation.

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

    This is too good! thank you!

  • @nguyenkhiem2318
    @nguyenkhiem2318 4 หลายเดือนก่อน +1

    This man should be a coveted treasure. Those dedication towards drawings and details, much appreciate it my man 🫡

  • @sravantipris3544
    @sravantipris3544 10 หลายเดือนก่อน +2

    i loved explanation👌👌

  • @chaman3679
    @chaman3679 6 หลายเดือนก่อน

    bro your explation is too goood keep it up

  • @Yokesh-cq5dp
    @Yokesh-cq5dp 3 หลายเดือนก่อน

    clean and neat explanation
    😄

  • @ajaychinni3148
    @ajaychinni3148 หลายเดือนก่อน

    Thanks you very much this was the best solution so far I watched

  • @tushardev1
    @tushardev1 10 หลายเดือนก่อน +1

    very good explanation , keep it up

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

    Well explained 💯

  • @tahsanrafi2884
    @tahsanrafi2884 2 วันที่ผ่านมา

    explanation was amazing

  • @shreeK-27
    @shreeK-27 6 หลายเดือนก่อน

    Great explanation thanks 👍👍

  • @lalanabiridi8358
    @lalanabiridi8358 8 หลายเดือนก่อน

    damnnnnn this channnel is just unbelievably good

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

    amazing explain 👏

  • @Sakethbhargavvlogs2000
    @Sakethbhargavvlogs2000 3 หลายเดือนก่อน

    too good explanation

  • @mandvibhadouriya9597
    @mandvibhadouriya9597 2 หลายเดือนก่อน

    Your teaching and problem solving skills are invaluable. Please upload more videos

    • @nikoo28
      @nikoo28  2 หลายเดือนก่อน

      New video every week

  • @matthewzarate8851
    @matthewzarate8851 หลายเดือนก่อน

    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

    • @nikoo28
      @nikoo28  29 วันที่ผ่านมา +1

      a dummy node is very helpful, when you want to preserve the head while manipulating your linked list

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

    easiest explanation🙂🙂

  • @atanukundu_10
    @atanukundu_10 4 หลายเดือนก่อน

    Thank you.

  • @exe.m1dn1ght
    @exe.m1dn1ght 4 หลายเดือนก่อน +1

    Sir, i got a little dizzy with this problem,i love it hahahah

  • @karanchauke
    @karanchauke 2 หลายเดือนก่อน

    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 . ... . .. . . .

  • @arpittripathi8089
    @arpittripathi8089 หลายเดือนก่อน

    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;
    }

  • @cnkumar20
    @cnkumar20 4 หลายเดือนก่อน

    I like the line “ if you start with head some where along you will loose your head”😅

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

    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.

  • @atharv9924
    @atharv9924 8 หลายเดือนก่อน +1

    If you know the problem start at 5:00 directly.

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

    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?

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

      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.

    • @RajeshKumar._.16
      @RajeshKumar._.16 6 หลายเดือนก่อน

      But every time while loops runs we are changing the point.next = swaps2, how it will hold the dummy.next@@nikoo28

  • @sushantsabnis5835
    @sushantsabnis5835 9 หลายเดือนก่อน +3

    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!

    • @shivanshsharma4916
      @shivanshsharma4916 9 หลายเดือนก่อน

      exactly same question bro... that dummy node is going over my head

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      @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

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      @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.

    • @shreypaidhungat6280
      @shreypaidhungat6280 8 วันที่ผ่านมา

      @@nikoo28 but you are doing point = swap1 in the next line, shouldn't that line itself take care of this?

  • @vaibhavsoni4219
    @vaibhavsoni4219 11 หลายเดือนก่อน +1

    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

    • @nikoo28
      @nikoo28  11 หลายเดือนก่อน

      Think about it...will it work for a list of length 10?

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

    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✨✨

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

      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!! :)

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

      @@nikoo28 thankuu😊

  • @vishaljaiswar6441
    @vishaljaiswar6441 8 หลายเดือนก่อน

    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

    • @nikoo28
      @nikoo28  8 หลายเดือนก่อน

      will try my best.

  • @tushar404
    @tushar404 หลายเดือนก่อน

    Cant get how in the end dummy->next will point to the new head of the list
    If any one.................

    • @nikoo28
      @nikoo28  29 วันที่ผ่านมา +1

      because dummy was created in the very beginning. The next was assigned to the head of the list

  • @ishankrajpal8231
    @ishankrajpal8231 2 หลายเดือนก่อน

    Should write the code on your own, this is like cramming up the code

  • @mohitchouhan3990
    @mohitchouhan3990 14 วันที่ผ่านมา

    took way too long to explain bro can be much much faster had to watch at 2X otherwise good explanation

    • @nikoo28
      @nikoo28  3 วันที่ผ่านมา

      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 :)

    • @zohaibahmad2752
      @zohaibahmad2752 2 วันที่ผ่านมา

      @@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

  • @I_W23
    @I_W23 5 หลายเดือนก่อน

    kam se kam shi code to diya kro

    • @nikoo28
      @nikoo28  5 หลายเดือนก่อน

      why do you feel the code is wrong? What test case fails for you?

    • @I_W23
      @I_W23 5 หลายเดือนก่อน

      @@nikoo28 it fails on all test cases
      As it does not change the original linked list

    • @nikoo28
      @nikoo28  5 หลายเดือนก่อน

      @@I_W23that cannot be possible. Check the code in the video description on my github profile. It passes perfectly on LeetCode

    • @I_W23
      @I_W23 5 หลายเดือนก่อน

      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

    • @nikoo28
      @nikoo28  4 หลายเดือนก่อน

      Garbage collector will take care of it. Declaring new won’t exhaust the memory.
      Sorry for getting back late