1public ListNode removeNthFromEnd(ListNode head, int n) {
2 ListNode dummy = new ListNode(0, head);
3 ListNode first = dummy;
4 ListNode second = dummy;
5
6 // Move first n+1 steps ahead
7 for (int i = 0; i <= n; i++) {
8 first = first.next;
9 }
10
11 // Move both until first reaches end
12 while (first != null) {
13 first = first.next;
14 second = second.next;
15 }
16
17 // Remove nth node
18 second.next = second.next.next;
19
20 return dummy.next;
21}