Simple one-pass java solution.


  • 0
    R
    public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode headFollow = head; 
            ListNode start = head;
            for(int i = 1; i < n; i++)
                 head = head.next;
            if(head.next == null)
                return headFollow.next;
            while(head.next.next != null) {
                head = head.next;
                headFollow = headFollow.next; 
            }
            headFollow.next = headFollow.next.next;
            return start;
        }

Log in to reply
 

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