class Solution {
public:
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode *x;
ListNode *y;
x=head;
y=head;
int y1=0;
while(x!=NULL)
{
x=x->next;
y1++;
}
y1=y1-n-1;
if(y1<0)
{
return head->next;
}
while((y1)--)
{
y=y->next;
}
y->next=y->next->next;
return head;
// write your code here
}
};
shui1
删除链表中倒数第n个节点 - C++
最新推荐文章于 2024-07-22 11:03:30 发布