Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:47:46+00:00 2026-06-18T01:47:46+00:00

I am doing a simple Link List in JavaScript(I’m a newbie) and I Have

  • 0

I am doing a simple Link List in JavaScript(I’m a newbie) and I Have the following code,

var List = function () {
  this.LinkedList = {
    "Head": {}
  };
};
List.prototype = {
  insert: function (element) {
    var Node = this.head();
    while (Node.hasOwnProperty("Node")) {
      Node = this.next(Node);
    }
    Node["Node"] = {
      "element": element
    };
  },
  remove: function (element) {
    var Node = this.head();
    while (Node.element != element) {
      Node = this.next(Node);
    }
    delete Node.element;
    Node = Node.Node; //overwriting Node with Node.Node
  },
  next: function (Node) {
    return Node.Node;
  },
  head: function () {
    return this.LinkedList.Head;
  },
  getList: function () {
    return this.LinkedList;
  }
};

When I am doing insertions it is doing fine like,

var myList = new List();
myList.insert(1);
myList.insert(5);
myList.insert(6);
myList.insert(2);

This gives me a List of,

    {
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 5,
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

Now when I do a delete, it is not giving the right List:

myList.remove(5);

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

What I want to get is like this:

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 6,
                "Node": {
                    "element": 2
                }
            }
        }
    }
}

Any Ideas on how to solve this? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:47:47+00:00Added an answer on June 18, 2026 at 1:47 am

    It’s because Node = Node.Node is not assigning the next node as the current node. You are merely assigning Node.Node to the variable Node. With that, you are NOT overwriting. In a sense, you only get “read privileges”.

    To get around this and get the benefits of passing references, modify the property of the object your variable is referencing to. That way, you have read and modify privileges, so to speak.

    A short example to explain your what happened in your code:

    //so we create an object
    var foo = {}
      , bar;
    
    //and assign a baz property carrying bam
    foo.baz = 'bam';
    
    //we reference foo.baz with bar
    bar = foo.baz;
    
    //so we expect that bar is bam
    console.log(bar); //bam
    
    //however, in this operation, we merely assign a value boom to bar
    //and not modify foo.baz
    bar = 'boom';
    
    //with bar modified in that way, foo.baz remains as bam
    console.log(bar); //boom
    console.log(foo.baz) //bam?
    

    So instead, here’s a simplified approach with a stripped version of the code:

    var List = function () {
        this.head = {}
    };
    List.prototype = {
        insert: function (element) {
            var node = this.head;
            while (node.next) {
                node = node.next
            }
            node.next = {
                "element": element
            };
        },
        remove: function (element) {
    
            //so we start with head
            var node = this.head;
    
            //basically, we assign node as the next node
            //that way, we'll be operating on next instead of node
            //so we can modify the values
    
            //while the next isn't the one we are after
            while (node.next.element != element) {
                //point to the next
                node = node.next;
            }
    
            //when we get our target, delete it
            delete node.next.element;
    
            //we don't assign to the variable node, but to a property
            //of the object node is referencing to
            node.next = node.next.next;
        }
    };
    

    And just as an aside, name your variables, properties and stuff verbosely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying create this simple ish to-do list web app using JavaScript &
I've been doing simple multi-threading in VB.NET for a while, and have just gotten
I have a WCF service hosted in IIS6. It is doing simple WebRequest. When
on my master page, I have referenced jquery file. I am doing simple hover
I'm doing a simple ajax query which retrieves a variable-length list of values as
I am having trouble doing something that is probably pretty simple! I have a
I'm using the following code as a link to delete a todo in a
I have a very simple link table set up, I need to delete rows
I feel like I'm doing something dumb here. I'm making a simple TODO list
I have a simple app that has a list of clients in one table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.