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:


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; = 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 { = elem;
      head = elem;
    length += 1;
    // Only change code above this line

var ll = new LinkedList();

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 Node(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);

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


var Tree = new BinaryTree();



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.

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 LinkList(){
        this.firstNode = null;
        this.count = 0;

    function Node(data = null){ = data; = null;

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

var list2 = new LinkList();

addNode('data2', list2);
addNode('data3', list2);
addNode('data4', 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.


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.