Detailed introduction to the implementation principle of linked lists
A linked list is a linear data structure. It is a collection of objects defined as nodes. But in a linked list, the nodes are stored in random locations in the memory and not in consecutive locations.
A node in a linked list consists of:
-
Data item.
-
The address of the next node.
class Node
{
int data;
Node *next;
};
This representation of the node is used to create each node in the list. The data field stores the element, while *next
is a pointer that stores the address of the next node.
The address of the first node is called the head, and the address of the last node is called the tail. The last node in a linked list points to the NULL
. Therefore, when the list is empty, the head node points to NULL
the . Linked lists do not require a prior declaration of size and can be increased in size dynamically. Inserting and deleting elements in a linked list is very easy. We do not need to move all the elements; it is enough to just change the pointers of the previous and next elements.
Linked Lists vs Arrays
Linked lists have a natural advantage over arrays because we don’t have to allocate a lot of memory upfront, but since the memory is not allocated contiguously, linked lists are not cache-friendly. They allow easy insertion and removal of elements with the help of pointers, but the storage space for each node is also doubled due to the space required for pointers. Linked lists also do not provide random access to elements. So, it is clear that there is no single winner, and both linked lists and arrays have their own pros and cons.
Arrays should be used when we have a small list and know the maximum number of elements we may store, whereas linked lists should be used when there is a large list that changes regularly.
Linked list traversal algorithm
Let head
be the first node of the linked list.
-
Initializes a pointer
head
to a node in a linked listcurr
. -
While
curr
has not yet reached the end of the list, i.e.curr
! =NULL
, do the following:- Prints the data stored in the current node.
-
curr=curr->next
;
Linked list traversal diagram
-
Initializes
head
a pointer to the nodecurr
with data value equal to2
. Prints the value2
. -
Move the pointer
curr
to the next node with value4
. Print the value4
. -
Move the pointer
curr
to the next node with value6
. Print the value6
. -
Move the pointer
curr
to the next node with value8
. Print the value8
. -
Move the pointer
curr
to the next node whose value is equal toNULL
.while
The loop termination condition is reached. Therefore, we have visited all the nodes.
Linked list traversal 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 printList(Node* head)
{
Node*curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
}
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);
head -> next-> next-> next-> next-> next = new Node(6);
printList(head);
return 0;
}
Linked list traversal algorithm complexity
Time Complexity
- Average situation
To traverse the complete linked list, we have to visit every node. Therefore, if a linked list has n
nodes, the average case time complexity of traversal is about O(n)
. The time complexity is about O(n)
.
- Best Case
The time complexity of the best case is O(n)
. It is the same as the time complexity of the average case.
- Worst case scenario
The worst case time complexity is O(n)
. It is the same as the best case time complexity.
Space complexity
The space complexity of this traversal algorithm is O(1)
, since curr
no additional space is required except for the pointer.
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.
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
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
Doubly Linked List
Publish Date:2025/03/18 Views:69 Category:ALGORITHM
-
Doubly linked list is a linear data structure. It is a collection of objects defined as nodes. But unlike a linked list, the node has two pointers, one is the previous pointer and the other is the next pointer. Just like the linked list nod
Link list deletion
Publish Date:2025/03/18 Views:189 Category:ALGORITHM
-
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. Iter
Linked List Merge Sort
Publish Date:2025/03/18 Views:138 Category:ALGORITHM
-
In this article, we will learn how to sort a linked list using merge sort algorithm. It is one of the most preferred algorithms to sort a linked list because slow pointer random access makes the performance of other algorithms worse (for ex
Linked list reversal
Publish Date:2025/03/18 Views:166 Category:ALGORITHM
-
A linked list is a linear data structure. A node in a linked list consists of: Data item. The address of the next node. class Node { int data; Node * next; }; This article will show you how to reverse a linked list given a pointer to the he