Linked List is basically chains of nodes where each node contains information such as data and a pointer to the next node in the chain. It is a popular data structure with a wide range of real-world applications. In this article, we will provide a complete introduction of Linked List, which will help you tackle any problem based on Linked List.
Table of Content
Linked List is a linear data structure which looks like a series of nodes , where each node has two parts: data and next pointer . Unlike Arrays, Linked List elements are not stored at a contiguous location. In the linked list there is a head pointer , which points to the first element of the linked list, and if the list is empty then it simply points to null or nothing.
Here are a few advantages of a linked list that is listed below, it will help you understand why it is necessary to know.
There are mainly three types of linked lists:
Singly Linked List is a type of linked list where each node has two parts: data and next pointer . The data part stores the information and the next pointer points to the next node of the linked list. The next pointer of the last node stores null as it is the last node of the linked list and there is no next node. Traversal of items can be done in the forward direction only due to the linking of every node to its next node.
Java
Python
JavaScript
The following are some basic operations performed on a Single Linked List:
Doubly Linked List is a type of linked list where each node has three parts: data, next pointer and previous pointer . The data part stores the information, the next pointer points to the next node of the linked list and the previous pointer points to the previous node of the linked list. The next pointer of the last node and the previous pointer of the first node stores null . Traversal of items can be done in the forward direction as well as backward direction due to the linking of every node to its next node as well as the previous node.
Java
Python
JavaScript
In a doubly linked list, we perform the following operations…
A circular linked list is a type of linked list in which the first and the last nodes are also connected to each other to form a circle, there is no NULL at the end.
The following operations are performed on a Circular Linked List
Below is the implementation of Linked List in different languages:
3 -> 4 -> 5 -> 6 C
3 -> 4 -> 5 -> 6 Java
3 -> 4 -> 5 -> 6 Python
3 -> 4 -> 5 -> 6 C#
3 -> 4 -> 5 -> 6 JavaScript
3 -> 4 -> 5 -> 6
Linked List: 2 3 4 5 6
Arrays are stored in contiguous location.
Linked Lists are not stored in contiguous location.
Fixed size (Dynamic Sized Arrays also internally use fixed sized arrays)
Only store elements no extra reference / pointer.
It stores both data and address of next node.
Elements can be accessed easily in O(1) time.
Elements can be access by traversing through all the nodes till we reach the required node.
Insertion and deletion operation is slower than Linked List.
Insertion and deletion operation is faster than Array.
Operation | Linked list | Array |
---|---|---|
Random Access | O(N) | O(1) |
Insertion and deletion at beginning | O(1) | O(N) |
Insertion and deletion at end | O(N) (If we maintain only head) | O(1) |
Insertion and deletion at a random position | O(N) | O(N) |
Here are some of the applications of a linked list:
Linked list are most commonly used to handle dynamic data elements. Linked list consists of nodes and a node consists of two fields one for storing data and other for keeping the reference of next node.
A linked list can be assumed as a garland that is made up of flowers. Similarly, a linked list is made up of nodes. Every flower in this particular garland is referred to as a node. In addition, each node points to the next node in this list, and it contains data (in this case, the type of flower).
There are some important advantages to using linked lists over other linear data structures. This is unlike arrays, as they are resizable at runtime. Additionally, they can be easily inserted and deleted.
The linked list is a linear data structure that stores data in nodes. these nodes hold both the data and a reference to the next node in the list. Linked are very efficient at adding and removing nodes because of their simple structure.
There are some advantages and disadvantages to both arrays and linked lists when it comes to storing linear data of similar types.
If any element is inserted/ deleted from the array, all the other elements after it will be shifted in memory this takes a lot of time whereas manipulation in Linked List is faster because we just need to manipulate the addresses of nodes, so no bit shifting is required in memory, and it will not take that much of time.
Singly-linked list (SLL) | Doubly linked list (DLL) |
---|---|
SLL nodes contains 2 field data field and next link field. | DLL nodes contains 3 fields data field, a previous link field and a next link field. |
In SLL, the traversal can be done using the next node link only. Thus traversal is possible in one direction only. | In DLL, the traversal can be done using the previous node link or the next node link. Thus traversal is possible in both directions (forward and backward). |
The SLL occupies less memory than DLL as it has only 2 fields. | The DLL occupies more memory than SLL as it has 3 fields. |
The Complexity of insertion and deletion at a given position is O(n). | The Complexity of insertion and deletion at a given position is O(n / 2) = O(n) because traversal can be made from start or from the end. |
Complexity of deletion with a given node is O(n), because the previous node needs to be known, and traversal takes O(n) | Complexity of deletion with a given node is O(1) because the previous node can be accessed easily |
A singly linked list consumes less memory as compared to the doubly linked list. | The doubly linked list consumes more memory as compared to the singly linked list. |
There are many advantages of the linked list compared to array, despite the fact that they solve the similar problem to arrays, we have also discussed the advantage, disadvantages, and its application, and we concluded the fact that we can use a linked list if we need the dynamic size of storage and list are good for adding and removing items quickly or for tasks that require sequence but are not suitable for querying or search elements in a large collection of data.
So, it becomes important that we should always keep in mind the positive and negative aspects of a data structure and how they relate to the problem you are trying to solve.
Related articles: