2024年7月27日 星期六

[3217] Delete Nodes From Linked List Present in Array

喔?!竟然是有點無痛的寫完XD 怎麼會呢!(嚇到自己)
莫非我要成為linked list 之神了嗎~(漫畫單手在下巴揮手圖:還早還早~)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* modifiedList(int* nums, int numsSize, struct ListNode* head) {
bool *hash =calloc (100000, sizeof(int));
for (int i=0; i<numsSize; i++)
{
hash[nums[i]]=true;
}
struct ListNode *ptr , *next, *pre;
ptr = head;
pre = NULL;
while (ptr != NULL)
{
next = ptr->next;
if (hash[ptr->val])//remove
{
ptr->next = NULL;
if (ptr == head)
head = next;
else
pre->next = next;
}
else
pre= ptr;
ptr = next;

}
return head;
}L

沒有留言:

張貼留言