哨兵链表例子_根据值删除链表 package linklist;public class leetcode203 {public static void main(String[] args) {ListNode listNode = new ListNode(1,new ListNode(2,new ListNode(3)));ListNode listNode1 = removeElements(listNode,2);System.out.println(listNode1);}public static ListNode removeElements(ListNode head, int val) { // 哨兵节点作为整个列表的头节点ListNode s = new ListNode(-1, head);ListNode p1 =s;ListNode p2 =s.next;while (p2!=null){if (p2.val==val){ // 删除 p2向后平移p1.next=p2.next;p2=p2.next;}else { // p1 p2 向后平移p1=p1.next;p2=p2.next;}}return s.next;}} class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) { this.val = val; }ListNode(int val, ListNode next) { this.val = val; this.next = next; }@Overridepublic String toString() {return "ListNode{" +"val=" + val +", next=" + next +'}';} } 用递归删除