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.
Subscribe to see which companies asked this question
1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7 * }8 */9 Public classSolution {Ten PublicListNode removeelements (ListNode head,intval) { One if(Head = =NULL)returnhead; AListNode ans =NewListNode (-1); -ListNode tmp =NewListNode (-1); -Ans.next =tmp; theListNode cur =head; - while(cur! =NULL){ - if(Cur.val! =val) { -Tmp.next =cur; +TMP =Tmp.next; - } +Cur =Cur.next; A } atTmp.next =NULL; - returnAns.next.next; - } -}
203. Remove Linked List Elements Java Solutions