java,双指针,时间N,空间1,时间击败100%,空间击败99.89%
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode fast = head,slow = head;
for(int i = 0;i<n;i++){
fast = fast.next;
}
if(fast==null) return head.next;
while(fast.next!=null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}