删除链表的倒数第n个节点
依旧是双指针; 移动指针后针对具体条件进行判断即可
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode fast=head;
ListNode slow=head;
for (int i=0;i<n;i++){
if(fast==null) return null;
fast=fast.next;
}
if(fast==null) return head.next;
else {
while (fast.next!=null){
fast=fast.next;
slow=slow.next;
}
}
slow.next=slow.next.next;
return head;
}