public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) {
return head;
}
int i = 0;
ListNode next = head;
while (i < n && next != null) {
next = next.next;
i++;
}
if (i < n) {
return null;
}
ListNode previewNode = head;
ListNode deleteNode = head;
while (next != null) {
next = next.next;
previewNode = deleteNode;
deleteNode = deleteNode.next;
}
if (deleteNode == head) {
head = head.next;
return head;
}
previewNode.next = deleteNode.next;
return head;
}