题目:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
答案:
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode p = head;
ListNode q = head;
while (n>0 && q!=null){
q = q.next;
n--;
}
if(n>0 || q==null){
head = head.next;
}else{
while (q!=null && q.next!=null){
p = p.next;
q = q.next;
}
p.next = p.next.next;
}
return head;
}