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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Has you met this question in a real interview?
/** * Definition for singly-linked list.* struct ListNode {* int val;* listnode *next;* listnode (int x): VA L (x), Next (NULL) {}*};*/classsolution{ Public: ListNode* Removeelements (ListNode *head,intval) {ListNode*pre_ptr = NULL, *cur_ptr =Head; while(cur_ptr) {if(Cur_ptr->val = =val) { if(NULL = =pre_ptr) {Head= head->Next; Deletecur_ptr; Cur_ptr=Head; } Else{pre_ptr->next = cur_ptr->Next; Deletecur_ptr; Cur_ptr= pre_ptr->Next; } } Else{pre_ptr=cur_ptr; Cur_ptr= cur_ptr->Next; } } returnHead; }};
[Leetcode] Remove Linked List Elements