Easy
Given the head
of a linked list and an integer val
, remove all the nodes of the linked list that has Node.val == val
, and return the new head.
Example 1:
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Example 2:
Input: head = [], val = 1
Output: []
Example 3:
Input: head = [7,7,7,7], val = 7
Output: []
Constraints:
[0, 104]
.1 <= Node.val <= 50
0 <= val <= 50
import com_github_leetcode.ListNode
/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
val sentinel = ListNode(-1)
sentinel.next = head
var next = head
var prev = sentinel
while (next != null) {
if (next.`val` == `val`) {
prev.next = next.next
} else {
prev = next
}
next = next.next
}
return sentinel.next
}
}