给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
链接: https://leetcode-cn.com/problems/remove-linked-list-elements/
解题思路
这个题目感觉是考对链表的基本操作,没有什么思路。如果该节点的下一个指针等于val,那就将这个指针指向下下个指针(curr.next = curr.next.next)。
public static ListNode removeElements(ListNode head, int val) {
ListNode curr = head;
while (curr != null && curr.next != null) {
if (curr.next.val == val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
if( head != null &&head.val == val){
head =head.next;
}
return head;
}