forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
42 lines (31 loc) · 776 Bytes
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
/// Author : liuyubobobo
/// Time : 2018-09-17
#include <iostream>
using namespace std;
///Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Recursive
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(!head)
return head;
if(head->val == val){
ListNode* node = head->next;
delete head;
return removeElements(node, val);
}
head->next = removeElements(head->next, val);
return head;
}
};
int main() {
return 0;
}