Linked List in JavaScript

What is a Linked List?
A linked list data structure involves a series of Nodes linked together. Each Node will have a data value and a reference to the next Node in the list. In the last Node, the reference will be set to null. Linked lists are not com…


This content originally appeared on DEV Community and was authored by Aden Eilers

What is a Linked List?
A linked list data structure involves a series of Nodes linked together. Each Node will have a data value and a reference to the next Node in the list. In the last Node, the reference will be set to null. Linked lists are not commonly used in front end web development, but they are still very popular for interview coding problems.

Here is a simple implementation of a Linked List

class Node {
  constructor(data, next = null) {
    this.data = data;
    this.next = next;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  // Inserts a Node into the Linked List
  insertFirst(data) {
    this.head = new Node(data, this.head);
  }

  // Returns the number of nodes
  size() {
    let node = this.head;
    let count = 0;

    while (node) {
      count++;
      node = node.next;
    }

    return count;
  }

  // Returns the first Node
  getFirst() {
    return this.head;
  }

  // Returns the last Node
  getLast() {
    if (!this.head.next) {
      return null;
    }
    let node = this.head;
    while (node) {
      if (!node.next) {
        return node;
      }
      node = node.next;
    }
  }

  // Removes all Nodes from the Linked List
  clear() {
    this.head = null;
  }

  // Removes the first Node from the Linked List
  removeFirst() {
    if (this.head) {
      this.head = this.head.next;
    }
  }
}

Here it is in use:

let list = new LinkedList();
list.insertFirst(1);
list.insertFirst(2);
list.insertFirst(3);
// list = {
//   head: {
//     data: 3,
//     next: {
//       data: 2,
//       next: {
//         data: 1,
//         next: null
//       }
//     }
//   }
// }

list.getFirst() // { data: 3, next:... }
list.getLast() // { data: 1, next: null }
list.size() // 3
list.removeFirst() // { head: { data: 2, next:... }}
list.clear() // { head: null }

You will notice that a Linked List in JavaScript is simply a series of nested objects. The list will always start with a head, and the last node reference will be null.

If you are preparing for coding interviews, here are a few coding challenges involving Linked Lists

Leave a comment if you have any questions or feedback.


This content originally appeared on DEV Community and was authored by Aden Eilers


Print Share Comment Cite Upload Translate Updates
APA

Aden Eilers | Sciencx (2022-03-21T00:16:00+00:00) Linked List in JavaScript. Retrieved from https://www.scien.cx/2022/03/21/linked-list-in-javascript/

MLA
" » Linked List in JavaScript." Aden Eilers | Sciencx - Monday March 21, 2022, https://www.scien.cx/2022/03/21/linked-list-in-javascript/
HARVARD
Aden Eilers | Sciencx Monday March 21, 2022 » Linked List in JavaScript., viewed ,<https://www.scien.cx/2022/03/21/linked-list-in-javascript/>
VANCOUVER
Aden Eilers | Sciencx - » Linked List in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/21/linked-list-in-javascript/
CHICAGO
" » Linked List in JavaScript." Aden Eilers | Sciencx - Accessed . https://www.scien.cx/2022/03/21/linked-list-in-javascript/
IEEE
" » Linked List in JavaScript." Aden Eilers | Sciencx [Online]. Available: https://www.scien.cx/2022/03/21/linked-list-in-javascript/. [Accessed: ]
rf:citation
» Linked List in JavaScript | Aden Eilers | Sciencx | https://www.scien.cx/2022/03/21/linked-list-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.