java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//注意初始化
int s_fast = 1;
ListNode dummyhead = new ListNode();
dummyhead.next = head;
ListNode fast = dummyhead;
ListNode slow = dummyhead;
while(fast!=null){
fast = fast.next;
if (s_fast++<=n+1){
continue;
}else{
slow = slow.next;
}
}
ListNode temp = slow.next.next;
slow.next = temp;
return dummyhead.next;
}
}