* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
struct ListNode* p0;
p0 = head;
int k=0;
int i=0;
struct ListNode* pre;
struct ListNode* cur;
struct ListNode* nex;
pre = NULL;
cur = head;
nex = NULL;
while(p0!=NULL)
{
k++;
p0 = p0->next;
}
if(n==k)
{
head = head->next;
}
else
{
while(i<(k-n))
{
nex = cur->next;
pre = cur;
cur = nex;
i++;
}
nex = cur->next;
pre->next = nex;
cur->next = NULL;
}
return head;
}