@@ -0,0 +1,1867 @@
+class ListNode %7B%0A    int val;%0A    ListNode next;%0A    ListNode(int x) %7B val = x; next = null;%7D%0A%7D%0A%0Apublic class RemoveNthFromEnd %7B%0A    /**%0A    * Given a linked list, remove the nth node from the end of list and return its head. %0A    */%0A    public ListNode removeNthFromEnd(ListNode head, int n) %7B%0A        ListNode nPrev = head;%0A        ListNode current = head;%0A        // to keep track of node previous to nth from the end%0A        int count = n + 1;%0A        // if no elements in list%0A        if (head == null) return null;%0A        // if n == 0, no node needs to be deleted%0A        if (n == 0) return head;%0A        while (current != null) %7B%0A            current = current.next;%0A            if (count == 0) nPrev = nPrev.next;%0A            else count--;%0A        %7D%0A        if (count == 0) nPrev.next = nPrev.next.next;%0A        // if the node is the head%0A        if (count == 1) head = head.next;%0A        return head;%0A    %7D%0A    %0A    public ListNode append (ListNode head, int val) %7B%0A        if (head == null) return new ListNode (val);%0A        ListNode cur = head;%0A        while (cur.next != null) cur = cur.next;%0A        ListNode newNode = new ListNode (val);%0A        cur.next = newNode;%0A        return head;%0A    %7D%0A    %0A    public String printList (ListNode head) %7B%0A        StringBuilder result = new StringBuilder ();%0A        while (head != null) %7B%0A            result.append (head.val + %22 %22);%0A            head = head.next;%0A        %7D%0A        return result.toString();%0A    %7D%0A    %0A    public static void main (String%5B%5D args) %7B%0A        RemoveNthFromEnd test = new RemoveNthFromEnd ();%0A        ListNode head1 = new ListNode (1);%0A//         test.append (head1, 2);%0A//         test.append (head1, 3);%0A//         test.append (head1, 4);%0A//         test.append (head1, 5);%0A        head1 = test.removeNthFromEnd (head1, 1);%0A        System.out.println(test.printList (head1));%0A    %7D%0A%7D

Remove nth node from end of list