LeetCode Remove Nth Node From End of List Solution Explained - Java

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

Komentáře • 66

  • @dengzhonghan5125
    @dengzhonghan5125 Před 4 lety +61

    At 3:57, you said fast is at 4, I think this is incorrect. Fast should be at 3 because it starts from 0 which is the dummy head. At that moment, slow should be at the beginning which should be 0, not 1.

    • @suvajitchakrabarty
      @suvajitchakrabarty Před 4 lety +11

      That was what confused me for a long time.

    • @zeeshannaqvi4091
      @zeeshannaqvi4091 Před 4 lety +2

      Yes that is correct - He made a mistake!

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

      I think he meant that in the linked list the value starts at 1, but the index is 0. so the index is at 3, but the value of the linked list is 4.

    • @Shiva-zy7jq
      @Shiva-zy7jq Před 3 lety

      Thanks. I was confused about it

    • @varshitbansal1713
      @varshitbansal1713 Před 3 lety

      thank you, it helped me too, was bugging me for the whole day!

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

    These leetcode problems really help a lot. We appreciate it!

  • @khew
    @khew Před 3 lety +4

    i think if Nick explain it with 0 node in the video would be more understandable because the actual node in the solution is appended with a new node 0

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

    If n== length of list then we will get null pointer exception for your solution

  • @sunnychung2607
    @sunnychung2607 Před 3 lety +4

    doesn't work when size of the list is same as n. simple if statement solves it but just pointing it out.

    • @honey-xr5kp
      @honey-xr5kp Před 5 měsíci

      works for me no if statement needed

  • @varaddesai8972
    @varaddesai8972 Před 3 lety +4

    Hey Nick, I solved this problem with a similar method. But it was written there that we should do it in one pass. So, by having a fast and a slow pointer, arent we making 2 passes to the linked list?

  • @tahseenrahman2922
    @tahseenrahman2922 Před 4 lety +17

    I understood the code, but can you please explain the logic(the reason behind doing it this way) in words. That would be really helpful.

    • @sarthakbhatia7639
      @sarthakbhatia7639 Před 4 lety +58

      We just need to go to the node before the one to be removed and to maintain the distance n from last we can take two-pointers and maintain the same distance between them, when one reaches null other reaches the required position and gets the job done

    • @sophiezhang6516
      @sophiezhang6516 Před 4 lety +2

      @@sarthakbhatia7639 Well explained

    • @sarthakbhatia7639
      @sarthakbhatia7639 Před 4 lety +1

      @@sophiezhang6516 Thankyou

    • @sindhusp8935
      @sindhusp8935 Před 3 lety +3

      @@sarthakbhatia7639 Excellent explanation. This should be in the video description. Kudos

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

      @@sarthakbhatia7639 Really good explanation. This is just so simple.

  • @diwakarj116
    @diwakarj116 Před 13 dny

    Superb your explanation is engaging and good

  • @anikethpaul3657
    @anikethpaul3657 Před 2 lety +1

    0:14 - 0:17 '"And all that other crap" lmaooooo

  • @shivendrakumarverma1514
    @shivendrakumarverma1514 Před 4 lety +3

    Hey why did you put 0th node in dummy node why not start the slow pointer and fast pointer from head instead??? Please explain

    • @ESwift
      @ESwift Před 4 lety +3

      This is just to prevent edge cases. For example, if the list provided has only 1 node, pointing to the head directly will be wrong.

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

    I have used a different method:
    step1: Reverse the whole linked list.
    step2: Traverse using count variable to print nth node.

    • @suganyam1672
      @suganyam1672 Před 3 lety

      I too thought the same approach, but is it considered as one pass solution?

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

      @@suganyam1672 no that's two pass

  • @tekken1935
    @tekken1935 Před 3 lety +5

    can we use temp instead of dummy_node ?

    • @CyberMew
      @CyberMew Před 3 lety +5

      It's just a name. Do whatever name make sense to you/reader.

  • @anirudhsingh576
    @anirudhsingh576 Před 4 lety +4

    why to use dummy?

  • @rebechkah
    @rebechkah Před rokem

    would it be possible to do this with a stack? if you pop all the nodes in, once node->next == nullptr, you could then pop the n times, store the toDelete->next as a temp, and the node left on the top would be the prev node which you can set next to delete's next? Or would this not actually work?

    • @NoobUCantBeat
      @NoobUCantBeat Před rokem

      sounds like it would work, did you try it out?

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

    What if the value one enters is the the value at the beginning

    • @Getentertainedp
      @Getentertainedp Před 2 lety

      I have the same doubt. I tried the same in JavaScript, It broke when i wanted to delete the first value

  • @pmolchanov2002
    @pmolchanov2002 Před 2 lety

    the loop should run to n-1, not n+1. Try it with n+1 and see it failed on test data.

  • @sahilbarua5202
    @sahilbarua5202 Před 2 lety +1

    how can u always get better than 100% of the solution ??

    • @don156
      @don156 Před 2 lety

      it is somewhat arbitrary I've gotten 30% faster and then 100% with the exact same code just running it different times in leetcode. Don't worry too much about the percentage

  • @alexpena9927
    @alexpena9927 Před 4 lety +1

    thanks nick

  • @yasuozed6110
    @yasuozed6110 Před 4 lety +1

    hey the discord is expired?

  • @LuisMorales-yx8di
    @LuisMorales-yx8di Před 3 lety +1

    premium problems on patreon? really...

  • @FahadKiani1
    @FahadKiani1 Před 3 lety

    The legend Nick White

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

    I solved this by traversing the whole list and at each point, I appended the node to an array. At the end, you get a an array containing pointers to each node of the list, and I just fetched the (len(arr) - n)th node from the array and deleted it.
    That's one pass and without using two pointers. Why doesn't anyone talk about this solution?

    • @adityapandey8245
      @adityapandey8245 Před 3 lety +4

      Because this array of yours uses extra space , which is not needed , buddy

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

      @@adityapandey8245 chill out with the buddy

    • @BetterEverydayHD
      @BetterEverydayHD Před rokem

      @@HialeahLiam stfu buddy

  • @samwong121
    @samwong121 Před 4 lety +3

    Is it possible to do it in one loop?

    • @suvajitchakrabarty
      @suvajitchakrabarty Před 4 lety

      This was done in one loop.

    • @user-le6ts6ci7h
      @user-le6ts6ci7h Před 2 lety

      Yep, you can do this with a single while loop, with a condition , start a counter, from 0 and when the value is greater than n+1 start moving the slow pointer and fast pointer else only move the fast pointer.

  • @abhinavkumar6584
    @abhinavkumar6584 Před 2 lety

    master luke????

  • @bala-OG
    @bala-OG Před rokem

    ALWAYS OP

  • @seanfitze4618
    @seanfitze4618 Před 3 lety

    oneth

  • @RASTstudio
    @RASTstudio Před 2 lety

    😯😯😯

  • @kabir5705
    @kabir5705 Před rokem

    niceeeeeeeeeeeeeee

  • @nathann4291
    @nathann4291 Před 3 lety

    not good!