0
0
DSA Pythonprogramming

Enqueue Using Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
Add new items to the end of a line by linking them one after another.
Analogy: Imagine people standing in a queue where each person holds the hand of the next person. When a new person arrives, they join at the end by holding the last person's hand.
front -> null
rear -> null

(empty queue with no nodes)
Dry Run Walkthrough
Input: Start with empty queue, enqueue values 10, then 20, then 30
Goal: Add each new value to the end of the linked list representing the queue
Step 1: enqueue 10 into empty queue
front -> [10] -> null
rear -> [10] -> null
Why: First node becomes both front and rear because queue was empty
Step 2: enqueue 20 at the end
front -> [10] -> [20] -> null
rear -> [20] -> null
Why: Link new node after rear and update rear pointer to new node
Step 3: enqueue 30 at the end
front -> [10] -> [20] -> [30] -> null
rear -> [30] -> null
Why: Link new node after rear and update rear pointer to new node
Result:
front -> 10 -> 20 -> 30 -> null
rear -> 30 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, value):
        new_node = Node(value)
        if self.rear is None:
            self.front = new_node
            self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def __str__(self):
        result = []
        current = self.front
        while current:
            result.append(str(current.data))
            current = current.next
        return ' -> '.join(result) + ' -> null'

# Driver code
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
print(queue)
if self.rear is None:
check if queue is empty to set front and rear to new node
self.front = new_node self.rear = new_node
initialize front and rear pointers to the first node
self.rear.next = new_node
link new node after current rear node
self.rear = new_node
update rear pointer to new node
OutputSuccess
10 -> 20 -> 30 -> null
Complexity Analysis
Time: O(1) because enqueue adds node directly at rear without traversal
Space: O(n) because each enqueue adds one new node to the linked list
vs Alternative: Compared to array-based queue, linked list enqueue avoids shifting elements and is always O(1)
Edge Cases
enqueue on empty queue
front and rear both point to the new node
DSA Python
if self.rear is None:
enqueue multiple elements
rear pointer moves forward each time, front stays at first node
DSA Python
self.rear = new_node
When to Use This Pattern
When you need to add items to the end of a queue efficiently, use enqueue with a linked list to keep O(1) insertion time.
Common Mistakes
Mistake: Not updating rear pointer after adding new node
Fix: Always set rear to new_node after linking it
Mistake: Not handling empty queue case separately
Fix: Check if rear is None and set front and rear to new_node
Summary
Adds new elements to the end of a queue using a linked list.
Use when you want efficient O(1) insertion at the queue's rear.
Remember to update rear pointer and handle empty queue case carefully.