力扣https://leetcode-cn.com/problems/remove-linked-list-elements/
考察基本点:
- 链表前后指针
- 链表遍历
package com.company.linked;
public class Solution6 {
public ListNode removeElements(ListNode head, int val) {
ListNode pre = new ListNode(0, head);
ListNode curr = head;
while (curr != null) {
if (curr.val == val) {
if (curr == head) {
head = curr.next;
}
pre.next = curr.next;
} else {
pre = pre.next;
}
curr = curr.next;
}
return head;
}
}