Follow

Follow

·Jun 27, 2022·

# 203. Remove Linked List Elements

## 題目敘述

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:

• The number of nodes in the list is in the range `[0, 10**4]`.
• `1 <= Node.val <= 50`
• `0 <= val <= 50`

## 解法解析

### 解法範例

#### Go

##### Recursive
``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func removeElements(head *ListNode, val int) *ListNode {
return nil
}
}
}
``````
##### Sentinel node
``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func removeElements(head *ListNode, val int) *ListNode {
var prev *ListNode = sentinel
for curr != nil {
if curr.Val == val {
prev.Next = curr.Next
} else {
prev = curr
}
curr = curr.Next
}
return sentinel.Next
}
``````

#### JavaScript

##### Recursive
``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} val
* @return {ListNode}
*/
var removeElements = function (head, val) {
};
``````
##### Sentinel node
``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} val
* @return {ListNode}
*/
var removeElements = function (head, val) {
const sentinel = new ListNode(0, head);
let prev = sentinel,
while (curr) {
if (curr.val === val) {
prev.next = curr.next;
} else {
prev = curr;
}
curr = curr.next;
}
return sentinel.next;
};
``````

#### Kotlin

##### Recursive
``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
if (head == null) return null
}
}
``````
##### Sentinel node
``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
var sentinel = ListNode(0)
var prev = sentinel
while (curr != null) {
if (curr.`val` == `val`) {
prev.next = curr.next
} else {
prev = curr
}
curr = curr.next
}
return sentinel.next
}
}
``````

#### PHP

##### Recursive
``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @param Integer \$val
* @return ListNode
*/
{
return null;
}
}
}
``````
##### Sentinel node
``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @param Integer \$val
* @return ListNode
*/
{
\$prev = \$sentinel;
while (\$curr != null) {
if (\$curr->val == \$val) {
\$prev->next = \$curr->next;
} else {
\$prev = \$curr;
}
\$curr = \$curr->next;
}
return \$sentinel->next;
}
}
``````

#### Python

##### Recursive
``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def removeElements(self, head: ListNode | None, val: int) -> ListNode | None:
return None
``````
##### Sentinel node
``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def removeElements(self, head: ListNode | None, val: int) -> ListNode | None:
while curr:
if curr.val == val:
prev.next = curr.next
else:
prev = curr
curr = curr.next

return sentinel.next
``````

#### Rust

``````
``````

#### Swift

##### Recursive
``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
guard head != nil else {
return nil
}
}
}
``````
##### Sentinel node
``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
var prev = sentinel
while curr != nil {
if curr!.val == val {
prev.next = curr!.next
} else {
prev = curr!
}
curr = curr!.next
}
return sentinel.next
}
}
``````