题目:
解决方法:快慢指针
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
if(head==null)
return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = head;
for(int i =0;i<n;i++){
if(fast==null)
return head;
fast = fast.next;
}
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}