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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 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

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

  • @dengzhonghan5125
    @dengzhonghan5125 4 ปีที่แล้ว +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 4 ปีที่แล้ว +11

      That was what confused me for a long time.

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

      Yes that is correct - He made a mistake!

    • @NilesMac2
      @NilesMac2 3 ปีที่แล้ว +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 3 ปีที่แล้ว

      Thanks. I was confused about it

    • @varshitbansal1713
      @varshitbansal1713 3 ปีที่แล้ว

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

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

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

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

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

  • @khew
    @khew 3 ปีที่แล้ว +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

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

    Superb your explanation is engaging and good

  • @varaddesai8972
    @varaddesai8972 3 ปีที่แล้ว +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?

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

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

    • @suganyam1672
      @suganyam1672 4 ปีที่แล้ว

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

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

      @@suganyam1672 no that's two pass

  • @tahseenrahman2922
    @tahseenrahman2922 4 ปีที่แล้ว +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 4 ปีที่แล้ว +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 4 ปีที่แล้ว +2

      @@sarthakbhatia7639 Well explained

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

      @@sophiezhang6516 Thankyou

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

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

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

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

  • @shivendrakumarverma1514
    @shivendrakumarverma1514 4 ปีที่แล้ว +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 4 ปีที่แล้ว +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.

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

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

  • @anirudhsingh576
    @anirudhsingh576 4 ปีที่แล้ว +4

    why to use dummy?

  • @pmolchanov2002
    @pmolchanov2002 2 ปีที่แล้ว

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

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

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

    • @don156
      @don156 2 ปีที่แล้ว

      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

  • @LuisMorales-yx8di
    @LuisMorales-yx8di 3 ปีที่แล้ว +1

    premium problems on patreon? really...

  • @rebechkah
    @rebechkah 2 ปีที่แล้ว

    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 ปีที่แล้ว

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

  • @FahadKiani1
    @FahadKiani1 3 ปีที่แล้ว

    The legend Nick White

  • @abhinavkumar6584
    @abhinavkumar6584 2 ปีที่แล้ว

    master luke????

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

    Is it possible to do it in one loop?

    • @suvajitchakrabarty
      @suvajitchakrabarty 4 ปีที่แล้ว

      This was done in one loop.

    • @user-le6ts6ci7h
      @user-le6ts6ci7h 3 ปีที่แล้ว

      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.

  • @bala-OG
    @bala-OG ปีที่แล้ว

    ALWAYS OP

  • @seanfitze4618
    @seanfitze4618 3 ปีที่แล้ว

    oneth

  • @sunnychung2607
    @sunnychung2607 3 ปีที่แล้ว +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 5 หลายเดือนก่อน

      works for me no if statement needed

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

    can we use temp instead of dummy_node ?

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

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

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

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

    • @Getentertainedp
      @Getentertainedp 2 ปีที่แล้ว

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

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

    hey the discord is expired?

  • @RASTstudio
    @RASTstudio 2 ปีที่แล้ว

    😯😯😯

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

    thanks nick

  • @kabir5705
    @kabir5705 ปีที่แล้ว

    niceeeeeeeeeeeeeee

  • @entropy7571
    @entropy7571 3 ปีที่แล้ว +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 3 ปีที่แล้ว +4

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

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

      @@adityapandey8245 chill out with the buddy

    • @BetterEverydayHD
      @BetterEverydayHD ปีที่แล้ว

      @@HialeahLiam stfu buddy

  • @nathann4291
    @nathann4291 3 ปีที่แล้ว

    not good!