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
PS: This problem feels no skill to say, choose a head pointer, a current pointer, a forward pointer. Simple list operation, the code is as follows:
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: Onelistnode* removeelements (listnode* head,intval) { AListNode * Root =head; -ListNode * prev =head; -ListNode * Curr =head; the while(Curr! =NULL) { - if(Curr->val = =val) { - if(prev = =Curr) -root = prev = Curr = curr->Next; + Else{ -Curr = curr->Next; +Prev->next =Curr; A } at}Else{ -Prev =Curr; -Curr = curr->Next; - } - } - returnRoot; in } -};
Leetcode oj:remove Linked list Elements (remove linked list element)