[Leetcode]删除链表中等于val 的所有结点
创始人
2024-06-02 08:40:56
0

力扣链接

方法一:

使用前后两个指针,cur指向当前位置,prev指向前一个位置,通过改变指向和释放结点来删除val

初步代码,还存在问题:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{struct ListNode* prev = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){prev = cur;cur = cur->next;}else{prev->next = cur->next;free(cur);// cur = cur->next;//错误,cur已经被释放,野指针cur = prev->next;}}return head;}

null pointer出现了空指针

通过测试用例代码走读分析问题:

如果第一个就是要删的值,也就是头删,会出现问题

所以这种情况要单独处理

最终代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{struct ListNode* prev = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){prev = cur;cur = cur->next;}else{if(prev == NULL){head = cur->next;free(cur);cur = head;}else{prev->next = cur->next;free(cur);//cur = cur->next;//和下一句等价,但是cur已经释放,这句会出现野指针cur = prev->next;}}}return head;//返回一个新的头,不需要用二级指针}

方法二:

把不是val的值尾插到新链表

初步代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{struct ListNode* newHead= NULL,* tail = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){//尾插if(tail == NULL){newHead = tail = cur;}else{tail->next = cur;tail = tail->next;}cur = cur->next;}else{struct ListNode* next = cur->next;free(cur);cur = next;}}return newHead;
}

通过走读代码我们发现,当最后一个结点的值为val时,释放节点后前面尾插的结点仍然指向最后一个结点,这里只需要将tail->next置空即可,修改后代码如下:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{struct ListNode* newHead= NULL,* tail = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){//尾插if(tail == NULL){newHead = tail = cur;}else{tail->next = cur;tail = tail->next;}cur = cur->next;}else{struct ListNode* next = cur->next;free(cur);cur = next;}}tail->next = NULL;return newHead;
}

但是代码仍然存在错误,运行如下:

显而易见,需要考虑链表为空的情况

改进后代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{if(head== NULL){return NULL;}struct ListNode* newHead= NULL,* tail = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){//尾插if(tail == NULL){newHead = tail = cur;}else{tail->next = cur;tail = tail->next;}cur = cur->next;}else{struct ListNode* next = cur->next;free(cur);cur = next;}}tail->next = NULL;return newHead;
}

报错:

这时代码直接指向最后一个else,此时tail为空,tail->next不合理,所以干脆前面不进行判断,而在后面对tail进行判断

最终代码如下:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{struct ListNode* newHead= NULL,* tail = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){//尾插if(tail == NULL){newHead = tail = cur;}else{tail->next = cur;tail = tail->next;}cur = cur->next;}else{struct ListNode* next = cur->next;free(cur);cur = next;}}if(tail){tail->next = NULL;}return newHead;
}

相关内容

热门资讯

你有哪些关于雨夜的故事? 你有哪些关于雨夜的故事?我记得那时还是在上中学的时候。那天晚上忽然间下了很大的雨。是很大的暴雨那一种...
找一本穿越滴小说 女主可以灵魂... 找一本穿越滴小说 女主可以灵魂出体 貌似有灵力,很强 原本是代嫁为妾 ,被指成主母,对男主的妾又煮又...
想写恐怖小说,该去哪里呢 想写恐怖小说,该去哪里呢看鬼片去鬼城
女孩子取什么名字好听又有内涵 女孩子取什么名字好听又有内涵婉静:温顺娴静。适用于女孩取名字。出自《后汉书•皇后纪上•明德马皇后》:...
云南畹町口岸迎来缅甸芒果进口高...   中新网昆明7月1日电 (赵吕媛)连日来,在位于中缅边境的云南畹町口岸,一辆辆装载着缅甸圣德龙芒果...
投教精品 | 一图看懂《上海证... (转自:南京证券阳光微讯)
喝大酒,吹大牛,大忽悠猜三个数... 喝大酒,吹大牛,大忽悠猜三个数字这三个数字是960。喝大酒,是酒的谐音9;吹大牛是牛的谐音6;大忽悠...
康辰药业跌2.08%,成交额1... 7月2日,康辰药业盘中下跌2.08%,截至10:11,报32.02元/股,成交1789.43万元,换...
受贿数额特别巨大!中国联通原副... 转自:扬子晚报中国联合网络通信集团有限公司原党组成员、副总经理曹兴信涉嫌受贿一案,由国家监察委员会调...
北仲沙龙 | 破局类案裁判差异... 转自:网络2025年6月27日下午,由北京仲裁委员会/北京国际仲裁院(以下称“北仲”)主办的2025...