import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
if (head == null || n == 0) return head ;
ListNode pre01 = new ListNode(-1) ;//根节点(可以消除对根节点被删除的情况)
pre01.next = head ;
ListNode fast = pre01 ;
int i = 1 ;
while(i <= n) {
fast = fast.next ;
i ++ ;
if(fast == null) {
return null ;
}
}
ListNode slow = head ;//slow最终为待删除的节点
ListNode slow_pre = pre01 ;//slow_pre最终为slow的前一个节点
while(fast.next != null) {
slow_pre = slow ;
slow = slow.next ;
fast = fast.next ;
}
slow_pre.next = slow_pre.next.next ;
return pre01.next ;
}
}