December 2023
Intermediate to advanced
504 pages
11h 43m
English
Some programming languages, such as Java, come with linked lists built into the language. Many languages don’t, but it’s fairly simple to implement them on our own.
Let’s create our own linked list using Python. We’ll use two classes to implement this: Node and LinkedList. Let’s create the Node class first:
| | class Node: |
| | |
| | def __init__(self, data): |
| | self.data = data |
| | self.next_node = None |
The Node class has two attributes: data contains the node’s primary value (for example, the string "a"), while next_node contains the link to the next node in the list. We can use this class as follows:
| | node_1 = Node("once") |
| | node_2 = Node("upon") |
| | node_3 = Node("a") |
| | node_4 = Node("time") |
| | |
| | node_1.next_node = node_2 ... |
Read now
Unlock full access