Concise Java Solution


  • 0
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode slow = head, fast = head;
        while (fast != null){
            if (n < 0) slow = slow.next;
            else n -= 1;
            fast = fast.next;    
        }
        if (n < 0) slow.next = slow.next.next;
        return n == 0 ? head.next : head;
    }
    

Log in to reply
 

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