Link list deletion
In this article, we will learn how to delete a node from a linked list.
Linked list deletion algorithm
Let head
be a pointer to the first node of the linked list and let temp
be the value of the node to be deleted from the linked list.
Iterative Algorithm
-
Initialize pointer
curr
to point tohead
for traversing the linked list andprev
set toNULL
to keep track of the node before when deletingtemp
. -
If the node to be deleted is
head
, that is,temp
!=NULL
&&curr->data
==temp
, thenhead
set tocurr->next
and deletecurr
. -
Otherwise, do the following until we reach the node we want to delete.
-
prev
=temp
。 -
temp
=temp->next
。
-
-
If
temp
==NULL
, return; -
Set
prev->next
totemp->next
, and deletetemp
the node.
Recursive Algorithm
-
If
head
==NULL
, then the linked list is empty and there is no element to be deleted. Therefore, return; -
If
head->data
==temp
, we found the node to delete.-
Store
head
in a temporary nodet
. -
Set
head
tohead->next
to remove the node. -
Delete
t
and return to the earlier recursive call.
-
Store
-
If none of the above conditions are met, recursively call
head->next
ondeleteNode()
to continue searching for nodes.
Linked list deletion diagram
Suppose we have the following linked list 1 -> 2 -> 3 -> 4
, and we want to remove 3
the node whose value is .
- Initializes a pointer to the node
with values
1
and .prev
head
curr
NULL
-
Move repeatedly
curr
until you reach a node with values of3
and .prev
2
-
To point to
prev
( ie2
) .curr->next
4
-
Delete the node
3
whose value iscurr
.
Linked List Removal Implementation
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
this->data = x;
this->next = NULL;
}
};
void deleteNode(Node*& head, int val)
{
if (head == NULL) {
cout << "Element not present in the list\n";
return;
}
if (head->data == val) {
Node* t = head;
head = head->next;
delete (t);
return;
}
deleteNode(head->next, val);
}
void deleteiter(Node** head, int x)
{
Node* temp = *head;
Node* prev = NULL;
if (temp != NULL && temp->data == x)
{
*head = temp->next;
delete temp;
return;
}
else
{
while (temp != NULL && temp->data != x)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;
}
}
void printList(Node* head)
{
Node*curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
cout << "\n";
}
int main()
{
Node* head = new Node(1);
head -> next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
printList(head);
deleteNode(head, 3);
printList(head);
deleteiter(&head, 4);
printList(head);
return 0;
}
Complexity of linked list deletion algorithm
Time Complexity
- Average situation
To delete 第 i 个
a node at position in a linked list, we have to visit i
nodes. Therefore, the time complexity is about O(i)
. Moreover, there are nodes in the linked list n
, so the time complexity in the average case is O(n/2)
or O(n)
. The time complexity is about O(n)
.
- Best Case
The best case scenario is when we want to remove the head of a linked list. The time complexity for the best case scenario is O(1)
.
- Worst case scenario
The worst case time complexity is O(n)
. This is the same as the average case time complexity.
Space complexity
In case of iterative implementation, the space complexity of this algorithm is O(1)
, since it does not require any data structures except temporary variables.
In a recursive implementation, the space complexity is 1/2 due to the space required for the recursive call stack O(n)
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/algorithm_9837.html
Related Articles
Convert a binary tree to a binary search tree
Publish Date:2025/03/18 Views:52 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
In-order descendants in a binary search tree
Publish Date:2025/03/18 Views:107 Category:ALGORITHM
-
The in-order descendant of a binary tree is the next node in the in-order traversal of the binary tree. So, for the last node in the tree, it is NULL . Since the in-order traversal of a binary search tree is a sorted array. The node with th
Binary Search Tree Deletion
Publish Date:2025/03/18 Views:93 Category:ALGORITHM
-
In the article Binary Search Trees: Searching and Inserting , we discussed how to insert an element into a binary search tree and how to search for a value in a binary search tree. In this article, we will discuss how to delete a node from
Binary Search Tree Check
Publish Date:2025/03/18 Views:79 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left children and right children. For a binary tree to be a BST, it must satisfy the following pr
Iterative insertion into a binary search tree
Publish Date:2025/03/18 Views:158 Category:ALGORITHM
-
In the previous article Binary Search Tree , we discussed the recursive method to insert a node in BST. In this article, we will discuss the iterative method to insert a node in BST. It is better than the recursive method because the iterat
Binary Search Tree
Publish Date:2025/03/18 Views:181 Category:ALGORITHM
-
A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary
Binary Tree Traversal
Publish Date:2025/03/18 Views:115 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
Circular Doubly Linked List
Publish Date:2025/03/18 Views:67 Category:ALGORITHM
-
A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin
Circular Linked List
Publish Date:2025/03/18 Views:172 Category:ALGORITHM
-
A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre