Step 3 of 6
50% CompleteInsertion Operations
Learn how to insert nodes at different positions in a linked list
Types of Insertion Operations
There are three main ways to insert a node into a linked list:
- Insert at the beginning (prepend)
- Insert at the end (append)
- Insert at a specific position
Let's explore each of these operations in detail.
1. Insert at the Beginning (Prepend)
Inserting a node at the beginning of a linked list is a simple operation:
- Create a new node with the given value
- Set the new node's next pointer to the current head
- Update the head to point to the new node
prepend(value) {// Create a new nodeconst newNode = new Node(value);// Set the new node's next pointer to the current headnewNode.next = this.head;// Update the head to point to the new nodethis.head = newNode;}
The linked list is empty
The linked list is empty
Time Complexity: O(1) - This is a constant-time operation as it only requires updating a few pointers, regardless of the list size.
2. Insert at the End (Append)
We've already seen the append operation in the previous tutorial. Here's a reminder of how it works:
- Create a new node with the given value
- If the list is empty, make the new node the head
- Otherwise, traverse to the end of the list
- Set the last node's next pointer to the new node
append(value) {// Create a new nodeconst newNode = new Node(value);// If the list is empty, make the new node the headif (!this.head) {this.head = newNode;return;}// Traverse to the end of the listlet current = this.head;while (current.next) {current = current.next;}// Set the last node's next pointer to the new nodecurrent.next = newNode;}
The linked list is empty
The linked list is empty
Time Complexity: O(n) - This operation requires traversing the entire list to find the last node, so it's linear in the size of the list.
3. Insert at a Specific Position
Inserting at a specific position combines elements of both previous operations:
- If position is 0, use the prepend method
- Create a new node with the given value
- Traverse the list to the node just before the desired position
- Update the pointers to insert the new node
insertAt(value, position) {// If position is 0, use prependif (position === 0) {this.prepend(value);return;}// Create a new nodeconst newNode = new Node(value);// Traverse to the node just before the desired positionlet current = this.head;let count = 0;while (current && count < position - 1) {current = current.next;count++;}// If position is beyond the length of the list, append to the endif (!current) {this.append(value);return;}// Update pointers to insert the new nodenewNode.next = current.next;current.next = newNode;}
The linked list is empty
The linked list is empty
Time Complexity: O(n) - In the worst case, we need to traverse to the end of the list, making this a linear-time operation.
Interactive Example
Try inserting nodes at different positions in the linked list below:
The linked list is empty
Use the controls to add nodes
Check Your Understanding
Next Steps
Now that you understand how to insert nodes into a linked list, let's move on to learning about deletion operations in the next tutorial.