Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Inserting a Node in a Linked List
A linked list is a linear data structure where elements (nodes) are stored in sequence, with each node containing data and a reference to the next node. Unlike arrays, linked lists allow efficient insertion and deletion at any position without shifting elements.
In this article, we'll explore how to insert nodes into a linked list using JavaScript, covering insertion at the beginning, specific positions, and the end of the list.
Basic Node Structure
First, let's define a simple Node class that represents each element in our linked list:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
Sample Input and Expected Outputs
Input: list = 1 -> 2 -> 3 -> 4 -> 5 -> null Input: new node = 7 Output (insert at beginning): 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> null Output (insert at position 3): 1 -> 2 -> 3 -> 7 -> 4 -> 5 -> null Output (insert at end): 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> null
Method 1: Inserting at the Beginning
To insert a node at the beginning, create a new node and make it point to the current head, then update the head pointer.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Insert at beginning
insertAtBeginning(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Helper method to display list
display() {
let result = "";
let current = this.head;
while (current !== null) {
result += current.data + " -> ";
current = current.next;
}
return result + "null";
}
}
// Example usage
const list = new LinkedList();
list.insertAtBeginning(5);
list.insertAtBeginning(4);
list.insertAtBeginning(3);
list.insertAtBeginning(2);
list.insertAtBeginning(1);
console.log("Original list:", list.display());
list.insertAtBeginning(7);
console.log("After inserting 7 at beginning:", list.display());
Original list: 1 -> 2 -> 3 -> 4 -> 5 -> null After inserting 7 at beginning: 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
Method 2: Inserting at a Specific Position
To insert at a specific position, traverse to the desired location and adjust the next pointers accordingly.
class LinkedList {
constructor() {
this.head = null;
}
insertAtPosition(data, position) {
if (position === 0) {
this.insertAtBeginning(data);
return;
}
const newNode = new Node(data);
let current = this.head;
// Traverse to the node before the insertion point
for (let i = 0; i ";
current = current.next;
}
return result + "null";
}
}
// Example usage
const list = new LinkedList();
[1, 2, 3, 4, 5].forEach(num => list.insertAtBeginning(num));
console.log("Original list:", list.display());
list.insertAtPosition(7, 3); // Insert 7 at position 3 (0-indexed)
console.log("After inserting 7 at position 3:", list.display());
Original list: 5 -> 4 -> 3 -> 2 -> 1 -> null After inserting 7 at position 3: 5 -> 4 -> 3 -> 7 -> 2 -> 1 -> null
Method 3: Inserting at the End
To insert at the end, traverse to the last node and update its next pointer to the new node.
class LinkedList {
constructor() {
this.head = null;
}
insertAtEnd(data) {
const newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
display() {
let result = "";
let current = this.head;
while (current !== null) {
result += current.data + " -> ";
current = current.next;
}
return result + "null";
}
}
// Example usage
const list = new LinkedList();
[1, 2, 3, 4, 5].forEach(num => list.insertAtEnd(num));
console.log("Original list:", list.display());
list.insertAtEnd(7);
console.log("After inserting 7 at end:", list.display());
Original list: 1 -> 2 -> 3 -> 4 -> 5 -> null After inserting 7 at end: 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> null
Complexity Analysis
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Insert at Beginning | O(1) | O(1) |
| Insert at Specific Position | O(n) | O(1) |
| Insert at End | O(n) | O(1) |
Complete Implementation
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
insertAtBeginning(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
insertAtPosition(data, position) {
if (position === 0) {
this.insertAtBeginning(data);
return;
}
const newNode = new Node(data);
let current = this.head;
for (let i = 0; i ";
current = current.next;
}
return result + "null";
}
}
// Demonstration
const list = new LinkedList();
console.log("Testing all insertion methods:");
// Build initial list: 1 -> 2 -> 3 -> null
[1, 2, 3].forEach(num => list.insertAtEnd(num));
console.log("Initial list:", list.display());
// Insert at beginning
list.insertAtBeginning(0);
console.log("After inserting 0 at beginning:", list.display());
// Insert at position 2
list.insertAtPosition(1.5, 2);
console.log("After inserting 1.5 at position 2:", list.display());
// Insert at end
list.insertAtEnd(4);
console.log("After inserting 4 at end:", list.display());
Testing all insertion methods: Initial list: 1 -> 2 -> 3 -> null After inserting 0 at beginning: 0 -> 1 -> 2 -> 3 -> null After inserting 1.5 at position 2: 0 -> 1 -> 1.5 -> 2 -> 3 -> null After inserting 4 at end: 0 -> 1 -> 1.5 -> 2 -> 3 -> 4 -> null
Conclusion
Linked list insertion is a fundamental operation with different time complexities depending on the position. Insertion at the beginning is most efficient (O(1)), while insertion at specific positions or the end requires traversal (O(n)). Understanding these operations is crucial for implementing efficient data structures and algorithms.
