好吧,参考的答案…感觉这种思路棒棒的…and以后要用java…
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
//定义两个指针,第一个保持指向最初的head指针
//使head指针移动,先到达访问第n个结点,第二个指针开始出发
//则当head访问完末尾结点时,第二个正好到达从后往前数的第n个结点
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode follower = dummy;
for(int i=0;i<n;i++){
if(head==null) return null;
head = head.next;
}
while(head!=null){
head = head.next;
follower = follower.next;
}
follower.next = follower.next.next;
return dummy.next;
}
}