Singly Linked list
Singly linked list is a collection of nodes where each node contains a value and a reference to the next node in the list.
Insert at Beginning
Inserting a node at the beginning of a singly linked list involves creating a new node, setting its next
pointer to the current head, and updating the head to point to the new node.
Time Complexity
The time complexity of inserting a node at the beginning of a singly linked list is O(1)
because we only need to update the head pointer.
Insert at End
Inserting a node at the end of a singly linked list involves creating a new node and traversing the list to find the last node, then updating the last node's next
pointer to point to the new node.
Time Complexity
The time complexity of inserting a node at the end of a singly linked list is O(n)
in the worst case because we need to traverse the entire list to find the last node.
Insert at Index
Inserting a node at a specific index in a singly linked list involves creating a new node and traversing the list to find the node at the desired index, then updating the pointers to insert the new node.
Time Complexity
The time complexity of inserting a node at a specific index in a singly linked list is O(n)
in the worst case because we may need to traverse the entire list to find the node at the desired index.
Delete from Beginning
Deleting a node from the beginning of a singly linked list involves updating the head pointer to point to the next node in the list and freeing the memory of the deleted node.
Time Complexity
The time complexity of deleting a node from the beginning of a singly linked list is O(1)
because we only need to update the head pointer.
Delete from End
Deleting a node from the end of a singly linked list involves traversing the list to find the second-to-last node, updating its next
pointer to NULL
, and freeing the memory of the last node.
Time Complexity
The time complexity of deleting a node from the end of a singly linked list is O(n)
in the worst case because we may need to traverse the entire list to find the second-to-last node.
Delete from Index
Deleting a node from a specific index in a singly linked list involves traversing the list to find the node at the desired index, updating the pointers to remove the node, and freeing the memory of the deleted node.
Time Complexity
The time complexity of deleting a node from a specific index in a singly linked list is O(n)
in the worst case because we may need to traverse the entire list to find the node at the desired index.
Search
Searching for a value in a singly linked list involves traversing the list and comparing each node's value with the target value.
Time Complexity
The time complexity of searching for a value in a singly linked list is O(n)
in the worst case because we may need to traverse the entire list to find the target value.
Last updated