techStackGuru

Linked List Overview


Data structures of this type consist of a series of nodes, each with a value and a reference to the node following it.

linked-list-1

Advantages of Linked Lists

  • Linked lists are a sort of dynamic data structure that allows for memory allocation and release, as well as expansion and trimming while a program is running.
  • The initial size is not required when dealing with linked lists.
  • The implementation of node operations for insertion and deletion is made simple by linked lists.
  • Items in the middle of the list can be added or removed.
  • Disadvantages of Linked Lists

  • In linked lists, search operations are slow.
  • As we need to keep the address of the next data, LinkedList consumes more memory.
  • Because we dont know the memory address of the prior pointers, we cant conduct a reverse traverse in the Singly LinkedList.
  • In Javascript, Linked Lists will look like this.

    const linkedlist = {
      head: {
          value: 1
          next: {
              value: 2                                            
              next: {
                  value: 3
                  next: {
                      value: 4
                      next: null	
                      }
                  }
              }
          }
      }
    } 

    There are 3 types of Linked Lists

  • Singly Linked Lists
  • Doubly Linked Lists
  • Circular Linked Lists
  • Singly Linked Lists

    Each node only has one pointer to the node after it.

    Implementation of ListNode

    class ListNode {
      constructor(data) {
          this.data = data
          this.next = null                
      }
    }

    Following that is a linked list class that keeps the lists head pointer.

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

    Construct a LinkedList class instance

    let list = new LinkedList();

    Lets pass value to node and print log

    class Node {
      constructor(val) {
          this.data = val;
          this.next = null;
      }
    }
    
    var node1 = new Node(1);
    
    console.log(node1) // Node { data: 1, next: null }

    Lets pass multiple value to node and print logs

    class Node {
      constructor(val) {
          this.data = val;
          this.next = null;
      }
    }
    
    let head;
      
    // Inserts a new Node 
    function add(new_data) {
          
    let add_node = new Node(new_data);
      
    // Make next Node as head
    add_node.next = head;
      
    // Point to new Node
    head = add_node;
    }
        
    add(1);
    add(2);
    add(3);
    
    /* In JavaScript, a node will look like this
    Node{
      data: 3,
      next: Node{
        data: 2,
        next: Node{
          data: 1,
          next: undefined
        }
      }
    }
    */ 

    Lets find length of the nodes now

    class Node {
    constructor(val) {
        this.data = val;
        this.next = null;
     }
    }
    
    let head;
      
    // Inserts a new Node 
    function add(new_data) {
          
      let add_node = new Node(new_data);
        
      // Make next Node as head
      add_node.next = head;
        
      // Point to new Node
      head = add_node;
    }
        
    add(1);
    add(2);
    add(3);
    
    function getLength() {
      let count = 0;
      let temp = head;
          
      while (temp != null) {
          count++;
          temp = temp.next;
      }
    return count;
    }
        
    console.log('Length is :', getLength()) // Length is : 3