203. Remove Linked List Elements
Remove all elements from a linked list of integers, that has value val.
Example
Given: 1---2--and 6---3---4---5, val = 6
Return: 1--2--and 3--4--5
Deletes all the elements specified in the linked list.
1) When you delete a linked list node, you should release the memory of the node in time to avoid memory leaks.
2) If the node value and the given value are consistent, the next node is assigned to *list, otherwise a node can be removed.
/** * definition for singly-linked list. * struct listnode { * int val; * struct listnode *next; * }; */struct listnode* removeelements (Struct ListNode* head, int val) { if ( head == NULL ) { return head; } struct ListNode **list = &head; while ( *list ) { if ( (*list)->val == val ) { struct listnode *delete = *list; * list = (*list)->next; free (delete); } else { list = & (*list)->next; } } return head;}
[leetcode]203. Remove Linked List Elements