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

fix: edit remove function of singly linked list #6067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

damla
Copy link

@damla damla commented Aug 24, 2021

Add condition to check if the element does not exist in the linked list.

Fixes issue:

  • If the user enters an element which does not exist in the linked list, it gives error as shown below:
/Users/test/other/interview/test.js:66
            while (currentNode.element !== element) {
                               ^

TypeError: Cannot read property 'element' of null
    at LinkedList.remove (/Users/damlakoksal/other/interview/test.js:66:32)
    at Object.<anonymous> (/Users/damlakoksal/other/interview/test.js:194:10)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
    ```

**Changes:**

- Add extra condition to check if the element is in the list or not.

Add condition to check if the element does not exist in the linked list.


// Check if head exist
if(!currentNode) console.log("list does not exist.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it'd not be useful to just log, you need to handle it. you can simple use if-else block to not proceed next if the node isn't exist.

Copy link
Author

@damla damla Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for mentioning! I forgot to add return in there. Will fix it 😊

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.

None yet

2 participants