Click to view the answer
The way is to introduce a `dummy` node, `dummy.next = head`.
## Complexity
- Time complexity: `O(N)`.
- Space complexity: `O(1)`.
## Java
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
var dummyHead = new ListNode();
dummyHead.next = head;
var node = dummyHead;
while (node.next != null) {
if (node.next.val == val) {
node.next = node.next.next;
} else {
node = node.next;
}
}
return dummyHead.next;
}
}
```
## Python
```python
# 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: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy_head = ListNode()
dummy_head.next = head
node = dummy_head
while node.next:
if node.next.val == val:
node.next = node.next.next
else:
node = node.next
return dummy_head.next
```
## C++
```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
auto dummyHead = new ListNode();
dummyHead->next = head;
auto node = dummyHead;
while (node->next != nullptr) {
if (node->next->val == val) {
auto next_node = node->next;
node->next = node->next->next;
delete next_node;
} else {
node = node->next;
}
}
return dummyHead->next;
}
};
```
## JavaScript
```javascript
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
var removeElements = function (head, val) {
const dummyHead = new ListNode()
dummyHead.next = head
let node = dummyHead
while (node.next != null) {
if (node.next.val == val) {
node.next = node.next.next
} else {
node = node.next
}
}
return dummyHead.next
};
```
## C#
```csharp
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RemoveElements(ListNode head, int val) {
var dummyHead = new ListNode();
dummyHead.next = head;
var node = dummyHead;
while (node.next != null) {
if (node.next.val == val) {
node.next = node.next.next;
} else {
node = node.next;
}
}
return dummyHead.next;
}
}
```
## Go
```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeElements(head *ListNode, val int) *ListNode {
dummyHead := &ListNode{}
dummyHead.Next = head
node := dummyHead
for node.Next != nil {
if node.Next.Val == val {
node.Next = node.Next.Next
} else {
node = node.Next
}
}
return dummyHead.Next
}
```
## Ruby
```ruby
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
def remove_elements(head, val)
dummy_head = ListNode.new
dummy_head.next = head
node = dummy_head
until node.next.nil?
if node.next.val == val
node.next = node.next.next
else
node = node.next
end
end
dummy_head.next
end
```
## Other languages
```java
// Welcome to create a PR to complete the code of this language, thanks!
```
Dear LeetCoders! For a better LeetCode problem-solving experience, please visit website [LeetCode.to](https://leetcode.to): Dare to claim the best practices of LeetCode solutions! Will save you a lot of time!
Original link: [203. Remove Linked List Elements - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions](https://leetcode.to/en/leetcode/203-remove-linked-list-elements).
GitHub repository: [leetcode-python-java](https://github.com/leetcode-python-java/leetcode-python-java).