Back to: Python Data Structure and Algorithims
0
Step 1: Create the Node Class
A node stores:
-
The value of the data.
-
A pointer (reference) to the next node.
Each node is like:
But in Python (using a class):
So every time you create a new node:
🪢 Step 2: Create the LinkedList Class (Constructor)
Now, the LinkedList class manages all nodes — it keeps track of:
-
The head (first node)
-
The tail (last node)
-
The length (total number of nodes)
Here’s the constructor:
So when you create a linked list:
👉 It automatically builds this structure in memory:
🧩 Step 3: Verify the Constructor Works
Let’s print the value of the head node:
✅ Full Working Code
Here’s the entire snippet you can run in VS Code:
🧠 Concept Recap
| Concept | Description |
|---|---|
| Node | Basic unit containing value and next. |
| LinkedList | A chain of nodes linked by pointers. |
| Head | Reference to the first node. |
| Tail | Reference to the last node. |
| Length | Total nodes in the list. |
Next up, you’ll typically add methods like:
-
.append(value)→ Add node at the end -
.prepend(value)→ Add node at the beginning -
.insert(index, value)→ Insert node at any position