Skip to content

Conversation

tejaswi0910
Copy link

@tejaswi0910 tejaswi0910 commented Oct 1, 2024

Describe your change:

This PR introduces a new function to handle the deletion of a node in a singly linked list when given access to only that specific node, not the head.

Changes include:
A delete() method added to the Node class that:
Copies the data from the next node into the current node.
Bypasses the next node to effectively "delete" the current node.
Raises an exception if the current node is the tail, since deleting the tail with this method is not possible.
Added custom CannotDeleteTailError exception for handling cases where the deletion is attempted on the tail node.

Motivation:
The problem of deleting a node from a singly linked list when only the reference to that node is provided is a common interview problem and a useful addition to this repository's linked list algorithms. This solution avoids traversal from the head node and directly modifies the node itself by manipulating pointers and data.

Added some sample test cases as well.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@tianyizheng02
Copy link
Contributor

We already have implementations of linked lists that support deleting nodes, for instance: data_structures/linked_list/singly_linked_list.py. Please check before you contribute an algorithm that the algorithm isn't already implemented in the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants