删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5
/** * definition for singly-linked list. * struct listnode { * int val; * struct listnode *next; * }; */ struct listnode* removeelements(struct listnode* head, int val) { struct listnode* s = (struct listnode* )malloc(sizeof(struct listnode)); s->next = head; struct listnode* pre = s; struct listnode* cur = head; while (cur) { if (cur->val == val) { pre->next = cur->next; } else { pre = cur; } cur = cur->next; } return s->next; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/608402.html