From 57a126a0ca2f988012f26826be32ec712cb78b27 Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Mon, 6 May 2024 10:10:20 +0800 Subject: [PATCH 1/3] add delete node in a linked list --- leetcode-cc/DeleteNodeInLinkedList.hpp | 27 ++++++++++++++++++++++++++ runtime-cc/src/registration.cc | 8 ++++++++ 2 files changed, 35 insertions(+) create mode 100644 leetcode-cc/DeleteNodeInLinkedList.hpp diff --git a/leetcode-cc/DeleteNodeInLinkedList.hpp b/leetcode-cc/DeleteNodeInLinkedList.hpp new file mode 100644 index 0000000..92ef006 --- /dev/null +++ b/leetcode-cc/DeleteNodeInLinkedList.hpp @@ -0,0 +1,27 @@ +#include "TestHelper.h" +#include "problem.h" +#include "solution.h" + +using namespace std; + +IMPLEMENT_PROBLEM_CLASS( + PDeleteNodeInLinkedList, 237, DIFFI_MEDIUM, TOPIC_ALGORITHMS, + "Delete Node In Linked List", + "There is a singly-linked list head and we want to delete a node node in " + "it. You are given the node to be deleted node. You will not be given " + "access to the first node of head.", + {"Linked List"}); + +class SDeleteNodeInLinkedList : public ISolution { + public: + size_t problemId() const override { return 237; } + string name() const override { + return ("Solution for " + string("Delete Node In Linked List")); + } + string location() const override { return __FILE_NAME__; } + int test() const override { return 0; }; + int benchmark() const override { return 0; } + + private: + void deleteNode(ListNode* node) const {} +}; diff --git a/runtime-cc/src/registration.cc b/runtime-cc/src/registration.cc index 2fdba64..5a77843 100644 --- a/runtime-cc/src/registration.cc +++ b/runtime-cc/src/registration.cc @@ -25,6 +25,7 @@ #include "../leetcode-cc/CourseSchedule.hpp" #include "../leetcode-cc/CourseScheduleII.hpp" #include "../leetcode-cc/DecodeWays.hpp" +#include "../leetcode-cc/DeleteNodeInLinkedList.hpp" #include "../leetcode-cc/DetIfTwoStrsAreClose.hpp" #include "../leetcode-cc/DeterIfStringHalvesAreAlike.hpp" #include "../leetcode-cc/DistinctSubsequences.hpp" @@ -966,5 +967,12 @@ const int registerAll(std::shared_ptr handle) { handle->registerSolution( []() -> ArcSolution { return std::make_shared(); }); + handle->registerProblem([]() -> ArcProblem { + return std::make_shared(); + }); + handle->registerSolution([]() -> ArcSolution { + return std::make_shared(); + }); + return 0; } From 07abc2a41c387beae2fd16c81b99d0041770333e Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Mon, 6 May 2024 10:21:37 +0800 Subject: [PATCH 2/3] impl delete node in a linked list --- leetcode-cc/DeleteNodeInLinkedList.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/leetcode-cc/DeleteNodeInLinkedList.hpp b/leetcode-cc/DeleteNodeInLinkedList.hpp index 92ef006..4394a27 100644 --- a/leetcode-cc/DeleteNodeInLinkedList.hpp +++ b/leetcode-cc/DeleteNodeInLinkedList.hpp @@ -23,5 +23,17 @@ class SDeleteNodeInLinkedList : public ISolution { int benchmark() const override { return 0; } private: - void deleteNode(ListNode* node) const {} + void deleteNode(ListNode* node) const { + if (node != nullptr) { + if (node->next) { + node->val = node->next->val; + + if (node->next->next) { + deleteNode(node->next); + } else { + node->next = nullptr; + } + } + } + } }; From e1db6e4fa41b7503585ecf0a91b61e41acf8aea2 Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Mon, 6 May 2024 10:28:53 +0800 Subject: [PATCH 3/3] fix rust clippy --- leetcode-rs/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/leetcode-rs/src/lib.rs b/leetcode-rs/src/lib.rs index c6c5c88..6fbdf19 100644 --- a/leetcode-rs/src/lib.rs +++ b/leetcode-rs/src/lib.rs @@ -1,7 +1,5 @@ //! Collections of problems and solutions in LeetCode -#![feature(exclusive_range_pattern)] - mod btree; mod list; pub mod problems;