-
Notifications
You must be signed in to change notification settings - Fork 932
add() function in linked-list.js #8
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
Comments
The short answer is... You need to update The long answer is... I'll use the example on the docs to explain this line better. Let's say we have the following linked list:
and we want to insert a node called "new" on the position So, After we create the "new" element, to add it to the middle of the list we need to update 4 links
In code, it looks like this: So after we create the node, we need to update 4 references:
|
@gabivlj Let me know if that explanation makes sense or if you still have doubts. I might add it to the main docs if this helps to understand. |
Shouldn’t it be current.previous = newNode instead of current.next.previous = newNode? |
@gabivlj You are totally right! It should be |
When inserting an item on the middle of a linked list one reference was not being updated properly. Fixed #8
In the add() function in linked-list.js I see the following line of code:
if (current.next) { current.next.previous = newNode; } // <7>
What is the reason behind this line of code? If we are inserting behind current, I don't know why would you set the current.next.previous to the node we wanna insert.
https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L83-L105
The text was updated successfully, but these errors were encountered: