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) {
ListNode cur=head;
int len=0;
while(cur!=null){
len++;
cur=cur.next;
}
int pos=len-n;
if(pos==0){
head=head.next;
return head;
}
cur=head;
ListNode prev=cur;
for(int i=0;i<pos;i++){
prev=cur;
cur=cur.next;
}
prev.next=cur.next;
return head;
}
}



京公网安备 11010502036488号