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;
}
}
leetcode
challenge
Here is the link for the problem:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
Top comments (4)
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
okay
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.
Thanks a lot Sir.....
I was finding but didn't get yet ...... thanks for giving me this trick ✨✨✨✨