DSA Linked Lists
As the name suggests, a linked list is one in which the nodes are connected to one another. Every node has a pointer and some data on it. Each node indicates the location of the subsequent node in the memory, which is how they are connected.
Linked Lists
A linked list is made up of nodes that contain data of some kind and a link, or pointer, pointing to the node after them.
One major advantage of linked lists is that nodes can be stored anywhere in memory that has empty space; unlike arrays, which need nodes to be stored consecutively after one another, linked lists do not require this. The fact that the remaining nodes in the list do not need to be moved when new or removed from the list is another great feature of linked lists.
Linked Lists vs Arrays
Maybe comparing linked lists and arrays can help you comprehend linked lists the best.
Unlike arrays, which are pre-existing data structures that we can utilize in the computer language, linked lists are linear data structures that we create ourselves and are made up of nodes.
While array elements do not have to store links to other elements, nodes in a linked list do.
Note: We’ll go into more detail on linked lists and arrays’ memory storage on the following page.
To help you better grasp linked lists, the table below contrasts them with arrays.
Arrays | Linked Lists | |
---|---|---|
An existing data structure in the programming language | Yes | No |
Fixed size in memory | Yes | No |
Elements, or nodes, are stored right after each other in memory (contiguously) | Yes | No |
Memory usage is low (each node only contains data, no links to other nodes) | Yes | No |
Elements, or nodes, can be accessed directly (random access) | Yes | No |
Elements, or nodes, can be inserted or deleted in constant time, no shifting operations in memory needed. | No | Yes |
The following page will concentrate on the memory storage methods of arrays and linked lists in order to further clarify these distinctions.