/**
* 重点:设置一个头结点,没有使用双指针,暴力算法
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode node = new ListNode(0);
node.next = head;
ListNode pre=node;
ListNode next=head;
int len = 0;
while(next!=null){
len++;
next = next.next;
}
int a = 0;
while(pre!=null){
if(a==len-n){
pre.next=pre.next.next;
break;
}
pre = pre.next;
a++;
}
return node.next;
} 
京公网安备 11010502036488号