先遍历一下整个链表的长度count,
然后用count-n+1计算待删除的结点在链表的位置
如何找到该结点的前一个结点
temp.next = temp.next.next进行删除
public static ListNode removeNthFromEnd(ListNode head, int n) {
ListNode temp = head;
int count = 1;
for(int i = 0; temp.next != null; i++) {
temp = temp.next;
count++;
}
int x = count - n + 1;
if(x == 1) {
head = head.next;
return head;
}else {
temp = head;
count = 1;
while(count != x-1) {
temp = temp.next;
count++;
}
temp.next = temp.next.next;
return head;
}
}