/**
-
Definition for singly-linked list.
-
public class ListNode {
-
int val;
-
ListNode next;
-
ListNode(int x) { val = x; }
-
}
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode domy = new ListNode(0);
domy.next = head;
int length = 0;
ListNode first = head;while(first != null){ length++; first = first.next; } length = length - n;//第n个节点前面的节点个数 first = domy; while(length > 0){//将first指针移动到第length-n个节点处 length--; first = first.next; } first.next = first.next.next;//删除第n个节点 return domy.next;
}
}