算法训练营第五天| 链表基础结构与操作

张开发
2026/4/18 7:22:32 15 分钟阅读

分享文章

算法训练营第五天| 链表基础结构与操作
1.题目链接https://leetcode.cn/problems/remove-linked-list-elements/ 视频讲解https://www.bilibili.com/video/BV18B4y1s7R92.题目建议 本题最关键是要理解 虚拟头结点的使用技巧这个对链表题目很重要。3.移除链表元素/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct ListNode* head, int val) { struct ListNode* dummy(struct ListNode*)malloc(sizeof(struct ListNode)); dummy-nexthead; struct ListNode* curr dummy; while(curr-next ! NULL){ if(curr-next-val val){ struct ListNode* tempcurr-next; curr-nextcurr-next-next; free(temp); }else{ currcurr-next; } }struct ListNode* newHeaddummy-next; free(dummy); return newHead; }

更多文章