Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-03-03 11:55:55
突然发现有点久没纪录= =
今天每日是
19. remove Nth Node from end of list
叫你删除从后面数的第N个节点
解法:
直接先看看有几个
然后再算要删的是从前面数的第几个
超级单纯的做法
等等来看其他人怎么写的
嘿嘿嘿
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
int all = 0 ;
ListNode* go = head;
while(go != NULL)
{
go = go->next;
all ++;
}
if(all == 1)return NULL;
if(n == all)return head->next;
go = head;
all = all - n;
ListNode* pre ;
for(int i = 0 ; i < all-1 ; i++)
{
pre = go;
go = go->next;
}
if(go->next->next != NULL)
{
go->next = go->next->next;
return head;
}
if(go->next->next == NULL)
{
go->next = NULL;
}
return head;
}
};
作者: v6SpcNwZQNtR (咕噜咕噜)   2024-03-03 12:04:00
大师
作者: JIWP (JIWP)   2024-03-03 12:05:00
大师,这题好难

Links booklink

Contact Us: admin [ a t ] ucptt.com