力扣刷题 19.删除链表的倒数第 N 个节点--day4
题目分析
这道题目比较简单, 熟练掌握单链表中删除节点的操作
解法
ListNode *removeNthFromEnd(ListNode *head, int n)
{
ListNode *dummyHead = new ListNode();
dummyHead->next = head;
ListNode *p = head;
int size = 0;
while (p)
{
size++;
p = p->next;
}
p = dummyHead;
for (int i = 0; i < size - n; i++)
{
p = p->next;
}
ListNode *temp = p->next;
p->next = temp->next;
head = dummyHead->next;
delete temp;
delete dummyHead;
return head;
}
标签:head,dummyHead,ListNode,19,next,链表,节点,倒数第
From: https://www.cnblogs.com/jianchuxin/p/17347942.html