mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
21
Easy/remove-linked-list-elements/solution.cpp
Normal file
21
Easy/remove-linked-list-elements/solution.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
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) {
|
||||
if (head == nullptr) {
|
||||
return nullptr;
|
||||
} else if (head->val == val) {
|
||||
return removeElements(head->next, val);
|
||||
} else {
|
||||
head->next = removeElements(head->next, val);
|
||||
return head;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user