Here is the link to the problem: Basic Linked List
Creating a linked list in Python involves defining a structure to store each element (node) and then linking each node to the next one. Here’s a simple step-by-step explanation:
- Define the Node Class:
- Each node in a linked list contains some data and a reference to the next node in the list.
- In Python, we can create a
Node
class to represent each element.
class Node:
def __init__(self, data):
self.data = data # Store the data
self.next = None # Reference to the next node (initially None)
- Define the LinkedList Class:
- We create a
LinkedList
class that manages the nodes. - This class will have a
head
attribute pointing to the first node in the list.
- We create a
class LinkedList:
def __init__(self):
self.head = None # Start with an empty list (no head node yet)
- Add Methods to the LinkedList:
- Let’s add a method to insert data at the end of the list.
def append(self, data):
new_node = Node(data) # Create a new node
if self.head is None: # If the list is empty, make this the head node
self.head = new_node
return
last = self.head
while last.next: # Traverse to the end of the list
last = last.next
last.next = new_node # Link the last node to the new node
- Display the List:
- We can add a method to print all the elements in the list.
def display(self):
current = self.head
while current: # Traverse through the list
print(current.data, end=" -> ")
current = current.next
print("None") # End of the list
- Using the Linked List:
- Now, we can create a linked list, add elements to it, and display it.
# Create a LinkedList
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
# Display the LinkedList
ll.display()
Output:
1 -> 2 -> 3 -> None
In summary:
- Each
Node
stores data and a link to the next node. - The
LinkedList
class manages these nodes, starting from thehead
. - We traverse the list by following the
next
pointers from one node to the next.