Step 4 of 6
67% CompleteDeletion Operations
Learn how to remove nodes from a linked list
Types of Deletion Operations
There are several ways to delete nodes from a linked list:
- Delete the first node (head)
- Delete the last node (tail)
- Delete a node at a specific position
- Delete a node with a specific value
Let's explore each of these operations in detail.
1. Delete the First Node (Head)
Deleting the first node is straightforward:
- Check if the list is empty
- Update the head to point to the second node
deleteFirst() {// Check if the list is emptyif (!this.head) {return;}// Update the head to point to the second nodethis.head = this.head.next;}
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 the head pointer.
2. Delete the Last Node (Tail)
Deleting the last node requires traversing the list to find the second-to-last node:
- Check if the list is empty or has only one node
- Traverse the list to find the second-to-last node
- Set the second-to-last node's next pointer to null
deleteLast() {// Check if the list is emptyif (!this.head) {return;}// If there's only one node, delete itif (!this.head.next) {this.head = null;return;}// Traverse to find the second-to-last nodelet current = this.head;while (current.next.next) {current = current.next;}// Set the second-to-last node's next pointer to nullcurrent.next = null;}
The linked list is empty
The linked list is empty
Time Complexity: O(n) - This operation requires traversing the list to find the second-to-last node, making it linear in the size of the list.
3. Delete a Node at a Specific Position
Deleting a node at a specific position combines elements of the previous operations:
- If position is 0, use the deleteFirst method
- Traverse the list to the node just before the target position
- Update the pointers to skip the node to be deleted
deleteAt(position) {// Check if the list is emptyif (!this.head) {return;}// If position is 0, delete the first nodeif (position === 0) {this.head = this.head.next;return;}// Traverse to the node just before the target positionlet current = this.head;let count = 0;while (current.next && count < position - 1) {current = current.next;count++;}// If the position is valid, update pointers to skip the nodeif (current.next) {current.next = current.next.next;}}
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.
4. Delete a Node with a Specific Value
Deleting a node with a specific value requires searching for the node first:
- Check if the list is empty
- If the head node has the target value, delete it
- Otherwise, traverse the list to find the node with the target value
- Update the pointers to skip the node to be deleted
delete(value) {// Check if the list is emptyif (!this.head) {return;}// If the head node has the target value, delete itif (this.head.value === value) {this.head = this.head.next;return;}// Traverse the list to find the node with the target valuelet current = this.head;while (current.next && current.next.value !== value) {current = current.next;}// If the node is found, update pointers to skip itif (current.next) {current.next = current.next.next;}}
The linked list is empty
The linked list is empty
Time Complexity: O(n) - This operation requires traversing the list to find the node with the target value, making it linear in the size of the list.
Interactive Example
Try deleting nodes from 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 delete nodes from a linked list, let's move on to learning about searching operations in the next tutorial.