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

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2024

Комментарии • 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 4 года назад +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

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

  • @amandapanda3750
    @amandapanda3750 2 года назад +2

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

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

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

  • @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 7 месяцев назад

      works for me no if statement needed

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

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

  • @tekken1935
    @tekken1935 4 года назад +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.

  • @anikethpaul3657
    @anikethpaul3657 2 года назад +1

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

  • @diwakarj116
    @diwakarj116 2 месяца назад

    Superb your explanation is engaging and good

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

  • @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 2 года назад

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

  • @Shiroyashasama
    @Shiroyashasama 3 года назад +1

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

    • @Getentertainedp
      @Getentertainedp 3 года назад

      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?

  • @LuisMorales-yx8di
    @LuisMorales-yx8di 3 года назад +1

    premium problems on patreon? really...

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

  • @FahadKiani1
    @FahadKiani1 4 года назад

    The legend Nick White

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

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

  • @abhinavkumar6584
    @abhinavkumar6584 2 года назад

    master luke????

  • @alexpena9927
    @alexpena9927 4 года назад +1

    thanks nick

  • @bala-OG
    @bala-OG Год назад

    ALWAYS OP

  • @kabir5705
    @kabir5705 Год назад

    niceeeeeeeeeeeeeee

  • @RASTstudio
    @RASTstudio 2 года назад

    😯😯😯

  • @seanfitze4618
    @seanfitze4618 3 года назад

    oneth

  • @nathann4291
    @nathann4291 3 года назад

    not good!