classSolution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNodedummy=newListNode(0); dummy.next = head; ListNodefirst= dummy; ListNodesecond= dummy; // Advances first pointer so that the gap between first and second is n nodes // apart for (inti=1; i <= n + 1; i++) { first = first.next; } // Move first to the end, maintaining the gap while (first != null) { first = first.next; second = second.next; } second.next = second.next.next; return dummy.next; } }