DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on • Updated on

19. Leetcode Solution in java

class Solution {
  public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode slow = head;
    ListNode fast = head;

    while (n-- > 0)
      fast = fast.next;
    if (fast == null)
      return head.next;

    while (fast.next != null) {
      slow = slow.next;
      fast = fast.next;
    }
    slow.next = slow.next.next;

    return head;
  }
}

Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

Here is the link for the problem:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/

Top comments (4)

Collapse
 
kalkwst profile image
Kostas Kalafatis

Nice solution!🎉🎉🎉

Next time you upload your solution for a leetcose challenge try to explain your thinking process, in order for other users to understand how you managed to solve the challenge instead of copying and pasting it

Collapse
 
chiki1601 profile image
Miss Pooja Anilkumar Patel

okay

Collapse
 
anderspersson profile image
Anders Persson • Edited

Tip: when insert your code, add java as keyword

´´´java

your code where

´´´

If you do this the code will be colored and easy to read.

Collapse
 
chiki1601 profile image
Miss Pooja Anilkumar Patel

Thanks a lot Sir.....

I was finding but didn't get yet ...... thanks for giving me this trick ✨✨✨✨