1 public static ListNode removeElement(ListNode head, int val) { 2 if (head == null) return null; 3 4 ListNode dummy = new ListNode(0); 5 dummy.next = head; 6 ListNode p = dummy; 7 8 while (p.next != null) { 9 if (p.next.val == val) { 10 ListNode next = p.next; 11 p.next = next.next; 12 } else { 13 p = p.next; 14 } 15 } 16 return dummy.next; 17 }
时间: 2024-10-15 21:02:13