Java Solution with three pointers

  • 0

    Move fast pointer n nodes ahead of curr pointer. Also maintain a prev pointer for deletion purpose. If the node to be deleted is the head node, return

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            if ( == null) {
                return null;
            ListNode prev=null;
            ListNode curr=head;
            ListNode fast=head;
            for (int i=1; i<=n; i++) {
                fast =;
            while (fast != null) {
                prev = curr;
                curr =;
                fast =;
            // fast is at the end and curr is nth node from the end
            if (curr == head) {
            return head;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.