203. Remove Linked List Elements

来源:互联网 发布:广州公交线路查询软件 编辑:程序博客网 时间:2024/06/11 22:57
/*Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.*//** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* removeElements(struct ListNode* head, int val) {    if(!head) return head;struct ListNode **h=&head;while(*h){if((*h)->val == val)while(*h && (*h)->val == val)*h=(*h)->next;elseh=&((*h)->next);}return head;}

原创粉丝点击