summaryrefslogtreecommitdiff
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
commit30b5ba4390d10223b09848350de303f85a4730f5 (patch)
treee88fe82ad309c3dcfef1fad3f84fcef4b16253f7
parent443e8ae2597f4f675dfff28e20391ba81f72c4ee (diff)
downloadsolutions-30b5ba4390d10223b09848350de303f85a4730f5.tar.gz
solutions-30b5ba4390d10223b09848350de303f85a4730f5.tar.bz2
solutions-30b5ba4390d10223b09848350de303f85a4730f5.zip
Add problem 203 .
-rw-r--r--cpp/203.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/cpp/203.cpp b/cpp/203.cpp
new file mode 100644
index 0000000..0f1bb55
--- /dev/null
+++ b/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;
+ }
+};