aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-08-05 16:25:29 +0800
committercrupest <crupest@outlook.com>2020-08-05 16:25:29 +0800
commit7522c4d912a4ef16b8ecb4a3d1854d3a4e31e640 (patch)
tree652f674e4c1862aaf375b6af9e5f309c9b610486
parent955d58a71d5b2bba5e902aca0a5bc8fc738960d5 (diff)
downloadcrupest-7522c4d912a4ef16b8ecb4a3d1854d3a4e31e640.tar.gz
crupest-7522c4d912a4ef16b8ecb4a3d1854d3a4e31e640.tar.bz2
crupest-7522c4d912a4ef16b8ecb4a3d1854d3a4e31e640.zip
import(solutions): Add problem 203 .
-rw-r--r--works/solutions/cpp/203.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/works/solutions/cpp/203.cpp b/works/solutions/cpp/203.cpp
new file mode 100644
index 0000000..0f1bb55
--- /dev/null
+++ b/works/solutions/cpp/203.cpp
@@ -0,0 +1,48 @@
+#include <cstddef>
+
+struct ListNode
+{
+ int val;
+ ListNode *next;
+ ListNode(int x) : val(x), next(NULL) {}
+};
+
+class Solution
+{
+public:
+ ListNode *removeElements(ListNode *head, int val)
+ {
+ if (head == NULL)
+ return NULL;
+
+ ListNode *last = NULL;
+ ListNode *current = head;
+
+ while (current != NULL)
+ {
+ if (current->val == val)
+ {
+ if (last == NULL)
+ {
+ auto temp = current;
+ current = current->next;
+ head = current;
+ delete temp;
+ }
+ else
+ {
+ auto temp = current;
+ current = current->next;
+ last->next = current;
+ delete temp;
+ }
+ }
+ else
+ {
+ last = current;
+ current = current->next;
+ }
+ }
+ return head;
+ }
+};