What is a Linked List?
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are called nodes. Each node contains a data field and a reference(link) to the next node in the list.
The elements in a linked list are linked using pointers as shown in the below image:
Types of Linked List
There are two types of linked lists:
- Singly Linked List
Doubly Linked List
There is also a variation of the linked list called a circular linked list.
Singly Linked List
In a singly linked list, each node contains a data field and a reference(link) to the next node in the list.
Doubly Linked List
In a doubly linked list, each node contains a data field, and two references (link) fields, one to the next node in the list and one to the previous node in the list.
Circular Linked List
A circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or a doubly circular linked list.
Advantages of Linked List
- Dynamic size
- Ease of insertion/deletion
Disadvantages of Linked List
- Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary searches with linked lists.
- Extra memory space for a pointer is required with each element of the list.
- Not cache-friendly. Since array elements are contiguous locations, there is the locality of reference which is not there in the case of linked lists.
Applications of Linked List
- Implementation of stacks, graphs and queues
- Used in undoing functionality at many places like editors, photoshop.
- Used in Hash Table implementation
- Used in Operating systems for process management
- Used in File System for file management
- Used in Memory management
- Used in Garbage Collection
Summary
- The Linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.
- Each node contains a data field and a reference(link) to the next node in the list.
- There are two types of linked lists: singly linked lists and doubly linked lists.
- There is also a variation of the linked list called a circular linked list.
- Advantages of linked list: Dynamic size, Ease of insertion/deletion
- Disadvantages of linked list: Random access is not allowed, Extra memory space for a pointer is required with each element of the list, Not cache-friendly.
- Applications of linked list: Implementation of stacks, graphs and queues, Used in undoing functionality at many places like editors, photoshop.
I'll be writing a lot on Data structures and Algorithms so follow and let's learn together 🚀.
Here's the repo for all the DSAs I've covered so far: github.com/princecodes247/20-days-of-dsa
Thanks for reading ✨️
Contributing
If you have any suggestions or improvements, feel free to open an issue or pull a request.