Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete node in a linked list #252

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions leetcode-cc/DeleteNodeInLinkedList.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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 {
if (node != nullptr) {
if (node->next) {
node->val = node->next->val;

if (node->next->next) {
deleteNode(node->next);
} else {
node->next = nullptr;
}
}
}
}
};
2 changes: 0 additions & 2 deletions leetcode-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Collections of problems and solutions in LeetCode

#![feature(exclusive_range_pattern)]

mod btree;
mod list;
pub mod problems;
Expand Down
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -966,5 +967,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<STheMazeII>(); });

handle->registerProblem([]() -> ArcProblem {
return std::make_shared<PDeleteNodeInLinkedList>();
});
handle->registerSolution([]() -> ArcSolution {
return std::make_shared<SDeleteNodeInLinkedList>();
});

return 0;
}
Loading