Skip to content
New issue

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

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

Already on GitHub? # to your account

A question about the implementation #24

Open
Dan-k391 opened this issue Nov 8, 2023 · 3 comments
Open

A question about the implementation #24

Dan-k391 opened this issue Nov 8, 2023 · 3 comments

Comments

@Dan-k391
Copy link

Dan-k391 commented Nov 8, 2023

As knowing the node to remove, why traverse the linked list instead of doing something such as:

target->value=target->next->value;
target->next=target->next->next;
@0rtz
Copy link

0rtz commented Nov 22, 2023

Previous item to target will still point to target

@ckwastra
Copy link

Recall that in our example, target is a pointer to the item to be removed, not a pointer to the previous item to be searched, which doesn't even exist in case of removing the first item.

@Basa198
Copy link

Basa198 commented Apr 22, 2024

That works as long as the element is not last in the list. You are basically rewriting the target element to be the same as the next and then deleting the next one, so the result is that the target is deleted. If there is no next element, there is nothing to duplicate and delete. We need a pointer to the previous item in this case, which is what ckwastra pointed out, I think.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants