Remove all elements from a linked list of integers, that has value val
.
Example
Given 1->2->3->3->4->5->3
, val = 3, you should return the list as1->2->4->5
Analysis:
Because the head of the list may contain Val values, it is cumbersome to operate, but if we build a dummy head, it is much easier to operate.
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 /** One * @param head a ListNode A * @param val an integer - * @return a ListNode - * cnblogs.com/beiyeqingteng/ the */ - PublicListNode removeelements (ListNode head,intval) { - if(Head = =NULL)returnhead; -ListNode Dummyhead =NewListNode (val +1); +Dummyhead.next =head; -ListNode current =Dummyhead; + A while(Current.next! =NULL) { at if(Current.next.val! =val) { -Current =Current.next; -}Else { -Current.next =Current.next.next; - } - } in returnDummyhead.next; - } to}
Reprint Please specify source: cnblogs.com/beiyeqingteng/
Remove Linked List Elements