Create a Linked List Class - Should have reference to newest node

Tell us what’s happening:
I cannot get the test “The previous node in your LinkedList class should have reference to the newest node created.” to pass.

When I create a new Linked List in the console and add 2 elements, the head is this:

{"element":"Foo","next":{"element":"Bar","next":null}}

Is this what is should be, or should “next” be pointing to something other than the full object?

Your code so far


function LinkedList() { 
  var length = 0; 
  var head = null; 

  var Node = function(element){
    this.element = element; 
    this.next = null; 
  }; 

  this.head = function(){
    return head;
  };

  this.size = function(){
    return length;
  };

  this.add = function(element){
    // Only change code below this line
    var elem = new Node(element);
    if(length < 1) {
      head = elem;
    } else {
      head.next = elem;
      head = elem;
    }
    length += 1;
    // Only change code above this line
  };
}

var ll = new LinkedList();
console.log(JSON.stringify(ll.head()));
ll.add("Foo");
console.log(JSON.stringify(ll.head()));
ll.add("Bar");
console.log(JSON.stringify(ll.head()));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Your implementation only works for one or two nodes. The new node should always be appended to the last node.

1 Like

Here’s one for ya.

(function(){

    function Node(data){
        this.data = data;
        this.left = null;
        this.right = null;
    }

    function BinaryTree(){
        this.root = null;
    }

    BinaryTree.prototype.push = function(data){
        var root = this.root;

        if (!root){
            this.root = new Node(data);
            return;
        }

        var newNode = new Node(data);
        var currentNode = this.root;
    
        while (currentNode){
            if (data < currentNode.data){
                if (!currentNode.left){
                    currentNode.left = newNode;
                    return;
                }
                currentNode = currentNode.left;
            } else {
                if (!currentNode.right){
                    currentNode.right = newNode;
                    return;
                }
                currentNode = currentNode.right;
            }
        }

    }

var Tree = new BinaryTree();
Tree.push(32);
Tree.push(32);

console.log(Tree);

})();

Here’s the problem!

Here’s a what you need to do

if  ( length < 1) {
  // assign the new Node to the head, you're doing it fine here.
} else {
  // if we reach here, that means we have a length
  // greater than 0 ( means we have 1 or more elements that already exists in the list)

  // now we need to get to the very LAST element of the list
  // how do we do that?
  //  hint: use a `while` loop! Loop until the `next` property of the element is null,
  // if the `next` property is null, it means that we have reached to that very last element
  // and then assign the new element to the last element's `next` property!
}
// we increment the length in both cases.
length++;

Let’s see if you can solve this now!

1 Like

I’m hating or anything, but binary trees are not equal to the linked lists. It would have been better if you could copy/paste a linked list solution at least.

Anyone can copy/paste the code from the internet. If one is asking a question here that means they want to understand what is wrong in THEIR code, what logic is missing.
If they intend on passing the challenge for the sake for just passing and not learning anything, copy/paste can do wonders in that case.

And there is nothing wrong providing a working solution, but explain it why your solution works and their doesn’t.

Following these little things will only make your answers better, and understandable.

Thank you!

  1. I wrote the code., it’s not copied pasted from internet.

  2. I pasted wrong one that I had saved.

(function(){

    function LinkList(){
        this.firstNode = null;
        this.count = 0;
    }

    function Node(data = null){
        this.data = data;
        this.next = null;
    }

    function addNode(data, list){
        var newNode = new Node(data);
        if (list.firstNode === null){
            list.firstNode = newNode;
            list.count++;
        }
        else {
            var currentNode = list.firstNode;
            while (currentNode.next !== null){
                currentNode = currentNode.next;
            }
            currentNode.next = newNode;
            list.count++;
        }
    }

var list2 = new LinkList();

addNode('data2', list2);
addNode('data3', list2);
addNode('data4', list2);

console.log(list2);


})();

On a side note., not everyone learns like you hussey. Some people prefer to see an example and figure it out for themselves. So how about you post your approach and I post mine? And let the viewers decide which one benefits them the most.

I didn’t mean to offend you. but anyways.

All I said was, if one can really understand things by looking at code, why would they wait for hours for a solution to be posted on a forum?

If someone is asking why THEIR code is not working, they really want to know what’s wrong in their code specifically. Everyone can google LinkedList Javascript and get hundreds of examples/tutorials.

I learn by seeing code a lot, I admit that. But I never post on a forum asking to send me a full solution (without explanation) so I can understand. That’d be outright stupid and waste of my and everybody’s time.

This thread isn’t about just one person. It’s for multiple people to learn from. If you looked at my badges you’d see I’ve helped more people than your logic assumes is best for people.

WOW! Impressive work! You deserve way more badges, seriously!

I wish there was a way to transfer badges, so I could transfer my couple of badges to you also, as I never strive for badges anyways. You’re truly an inspiration for me now :pray:. Thank you!

You don’t have to strive for badges. They’re awarded by people who benefit from your posts. And by the looks of it., you’ve been on this site 4-5 months longer than I, yet you’ve helped far fewer people.

Perhaps if you devoted more of your time to helping people on threads rather than critiquing those who are far more appreciated than you on site you, you might actually be worth something.

okay, so please both stop. Honestly, until i saw this becoming Hatfields and McCoys, i’d never even looked at my badges, and after this i probably never will again. I think the question has been answered, so good on you both. If you want to continue to discuss your differences, perhaps by PM or on the Discord or Slack. Elsewhere than a legit request for help.

3 Likes

I see now…I was confused by what “head” represented. I thought it was supposed to point to the last node, so I was altering “head”. “Head” is actually supposed to point to the first node, so that means I needed to set a different variable to track the current node. Thank you to both @husseyexplores & @kerafyrm02 for your help.