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
12345678910111213141516171819from 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!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 4.35
Linked List Data Structure
Swipe to show menu
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
12345678910111213141516171819from 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!