Remove all elements from a linked list of integers, that has value Val. Examplegiven:1 2--and 6--and 3--4--5--and 6, val = 6return:1--and 2--and 3--and 4-- 5
Test instructions: Deleting nodes in a linked list
/** * definition for singly-linked list. * public class listnode { * int val; * ListNode Next; * listnode (int x) { val = x; } * } */public class solution { //////head points to the first Data node. There is no additional application for public listnode removeelements before the data (listnode head, int Val) { ListNode cur=head; listnode newhead=new listnode (1); newhead.next=head; ListNode pre=newhead; // pre.next=head; while (Cur!=null) { if (Cur.val==val) { pre.next=cur.next; }else{ pre=pre.next; } cur=cur.next; } // system.out.println (Head.val); return newhead.next; }}
PS: To prevent the head from being deleted, the new head points to the original head ... ..... ... ..... ..... ..... ..... ..... ..... ..... ..... .... ..... ........ It's disgusting.
Leetcode 203. Remove Linked List Elements Java language