/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* Newhead = head;
ListNode* p=head;
if(head==NULL) return NULL;
for(int i =0;i<n;i++)
{
Newhead=Newhead->next;
}
if(Newhead==NULL)
return head->next;
while(Newhead->next!=NULL)
{
Newhead=Newhead->n
关于链表的删除和头指针的易错点
最新推荐文章于 2022-08-22 21:05:28 发布