思路:
删除从列表开头数起的第 (L - n + 1)个结点;
把第 (L - n) 个结点的 next 指针重新链接至第(L−n+2) 个结点
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
int length = 0;
ListNode first = head;
while (first != null) {
length++;
first = first.next;
}
length = length - n;
first = dummy;
while (length > 0) {
length--;
first = first.next;
}
first.next = first.next.next;
return dummy.next;
}