import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
//第一步计算长度
if(head == null || head.next == null){
return null;
}
int length = 0;
ListNode temp = head;
while(temp != null){
length ++;
temp = temp.next;
}
if(length == n){
return head.next;
}
//第二步 找到倒数第n个节点的前一个节点
int target = length - n -1;
temp = head;
for(int i = 0;i<target; i++){
temp = temp.next;
}
temp.next = temp.next.next;
return head;
}
}