JavaScript Program for Inserting a Node in a Linked List


Linked lists are data structures with varying lengths and any node can be deleted or added to the linked list. In this tutorial, we are going to implement a complete program for inserting a node in a linked list with space and time complexity. Let’s first understand the problem statement.

Introduction to Problem

In the given problem we are given a linked list and as we can change the size of the linked list by adding or removing a node from the linked list, we are going to add or insert a node in a linked list.

In the linked list, we have three different positions in which we can add the new node: the before front node, after the last node, and at the middle of the linked list. For example, the given linked list is −

1 -> 2 -> 3 -> 4 -> 5 -> null and we have to add a random node with value 9. So there are many cases to add the node like −

  • Adding node at the starting − 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> null

  • Adding node in the middle − 1 -> 2 -> 3 -> 7 -> 4 -> 5 -> null

  • Adding node at the ending − 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> null

Let’s see the approach to implement the following tasks −

Adding Nodes at The Starting of the Linked List

Example

To add the node at the start of the linked list we have to create the new node and pass the head of the linked list as the next node to the new node, then shift the head to the new node, add the new node to the linked list at the start.

// creating the linked list node
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}
function push(tail, data){
   var new_node = new Node(data);
   tail.next = new_node;
   tail = tail.next;
   return tail
}
function add(data) {
   var new_node = new Node(data);
   new_node.next = head;
   return new_node;
}

var head = new Node(1);
var tail = head;
tail = push(tail, 2)
tail = push(tail, 3)
tail = push(tail, 4)
tail = push(tail, 5)

head = add(7);
var data = 0;
while(head != null) {
   data = data + head.value + " -> ";
   head = head.next;
}
console.log("Linked List after adding a node at starting: ")
console.log(data + "null")

The time complexity of the above code is O(1) because we have to just move one pointer and similarly no extra space is used making space complexity O(1).

Adding Nodes in The Middle of the Linked List

Example

To add the node in the middle of the linked list we have to create the new node and pass the node before we have to add the new node of the linked list as the next node to the new node, this adds the new node to the linked list in the middle.

// creating the linked list node
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}
function push(tail, data) {
   var new_node = new Node(data);
   tail.next = new_node;
   tail = tail.next;
   return tail
}
function add(data,head) {
   var new_node = new Node(data);
   var temp = head;
   while(temp.value != 3) {
      temp  = temp.next;
   }
   new_node.next = temp.next;
   temp.next = new_node;
   return head;
}
var head = new Node(1);
var tail = head;
tail = push(tail, 2)
tail = push(tail, 3)
tail = push(tail, 4)
tail = push(tail, 5)
head = add(7,head);
var data = 0;
while(head != null) {
   data = data + head.value + " -> ";
   head = head.next;
}
console.log("Linked List after adding node in middle:")
console.log(data + "null")

The time complexity of the above code is O(N) because we have to move to the node before which we have to add the new node. The space complexity of the above process is O(1) because we are not using any extra space.

Adding Nodes at the End of the Linked List

Example

To add the node at the end of the linked list we have to create a new node and add the node after the tail node and move the tail node to the next node.

// creating the linked list node
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}
function push(tail, data) {
   var new_node = new Node(data);
   tail.next = new_node;
   tail = tail.next;
   return tail
   }

function add(data) {
   var new_node = new Node(data);
   tail.next = new_node;
   tail = tail.next
   return tail;
}
var head = new Node(1);
var tail = head;
tail = push(tail, 2)
tail = push(tail, 3)
tail = push(tail, 4)
tail = push(tail, 5)

tail = add(7);
var data = 0;
while(head != null){
   data = data + head.value + " -> ";
   head = head.next;
}
console.log("Linked List after adding a node at the end: ")
console.log(data + "null")

The time complexity of the above code is O(1) because we have to just move one pointer and similarly no extra space is used making space complexity O(1).

Conclusion

In the above tutorial, we have learned how to add a new node in the existing linked list in three ways possible. We have seen the proper code with explanation and the time and space complexities. To add a node in the middle of the linked list takes O(N) time while for the other two cases its O(1) and for all three possibilities space complexity is O(1).

Updated on: 24-Mar-2023

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements