Course Content
Algorithms and Data Structures Overview
Algorithms and Data Structures Overview
Linked List Data Structure
A linked list is a data structure consisting of a sequence of elements called nodes, where each node contains data and a reference to the next node in the sequence.
Unlike arrays, linked lists do not have a fixed size in memory and do not store elements in contiguous memory locations. Instead, they use pointers to connect nodes, allowing for dynamic memory allocation and efficient insertion and deletion of elements.
The next section of the code illustrates this concept.
Linked list vs Array
Linked list implementation
from lolviz import * from IPython.display import display_png class Node: def __init__(self, data): self.value = data self.next = None # Let's create some nodes node1 = Node(1) node2 = Node(2) node3 = Node(3) # Then let's couple them into a linked list node1.next = node2 node2.next = node3 display_png(objviz(node1))
Note
Pay attention that the built-in
list
Python structure is not a list conceptually - thelist
structure is implemented as a dynamic array that can hold elements of various data types. It is similar to an array but with additional functionalities and optimizations.
In contrast to an array where we have direct access to any item, in the linked list, we have direct access only to the first item, and we can access any other item only by the chain of references.
Thanks for your feedback!