Bird
Raised Fist0
DSA Goprogramming

Enqueue Using Linked List in DSA Go

Choose your learning style10 modes available
Mental Model
Enqueue means adding an item to the end of a line. Using a linked list, we add a new node at the tail so the order stays correct.
Analogy: Imagine a queue of people waiting to buy tickets. When a new person arrives, they stand at the end of the line. The linked list tail pointer helps us find the end quickly to add the new person.
head -> 1 -> 2 -> 3 -> null
                 ↑tail
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, enqueue value 4
Goal: Add value 4 at the end of the linked list queue
Step 1: Create new node with value 4
head -> 1 -> 2 -> 3 -> null
                 ↑tail    4 -> null
Why: We need a new node to add to the queue
Step 2: Set current tail's next pointer to new node
head -> 1 -> 2 -> 3 -> 4 -> null
                      ↑tail    
Why: Link the last node to the new node to keep the chain
Step 3: Update tail pointer to new node
head -> 1 -> 2 -> 3 -> 4 -> null
                           ↑tail
Why: Tail must point to the new last node for future enqueues
Result:
head -> 1 -> 2 -> 3 -> 4 -> null
                           ↑tail
Annotated Code
DSA Go
package main

import "fmt"

type Node struct {
    value int
    next  *Node
}

type Queue struct {
    head *Node
    tail *Node
}

func (q *Queue) Enqueue(val int) {
    newNode := &Node{value: val}
    if q.tail == nil {
        // Queue is empty, new node is head and tail
        q.head = newNode
        q.tail = newNode
        return
    }
    // Link current tail to new node
    q.tail.next = newNode
    // Update tail to new node
    q.tail = newNode
}

func (q *Queue) Print() {
    curr := q.head
    for curr != nil {
        fmt.Printf("%d", curr.value)
        if curr.next != nil {
            fmt.Print(" -> ")
        }
        curr = curr.next
    }
    fmt.Println(" -> null")
}

func main() {
    q := &Queue{}
    q.Enqueue(1)
    q.Enqueue(2)
    q.Enqueue(3)
    q.Print() // Before enqueue 4
    q.Enqueue(4)
    q.Print() // After enqueue 4
}
newNode := &Node{value: val}
Create new node with given value
if q.tail == nil {
Check if queue is empty
q.head = newNode q.tail = newNode return
Set head and tail to new node if empty
q.tail.next = newNode
Link current tail node to new node
q.tail = newNode
Update tail pointer to new node
OutputSuccess
1 -> 2 -> 3 -> null 1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(1) because we add the new node directly at the tail without traversing the list
Space: O(1) because we only create one new node for each enqueue
vs Alternative: Compared to adding at head or traversing to tail (O(n)), using tail pointer makes enqueue fast and efficient
Edge Cases
Empty queue
New node becomes both head and tail
DSA Go
if q.tail == nil {
Single element queue
Tail's next points to new node, tail updates correctly
DSA Go
q.tail.next = newNode
When to Use This Pattern
When you need to add items to the end of a queue efficiently, look for the enqueue using linked list pattern with a tail pointer to avoid slow traversal.
Common Mistakes
Mistake: Not updating the tail pointer after adding the new node
Fix: Always set tail = newNode after linking the new node
Mistake: Not handling empty queue case, causing nil pointer errors
Fix: Check if tail is nil and set head and tail to new node in that case
Summary
Adds a new element at the end of a linked list queue using a tail pointer.
Use when you want fast enqueue operations without traversing the whole list.
Keep track of the tail node to add new nodes in constant time.

Practice

(1/5)
1. What does the enqueue operation do in a queue implemented using a linked list?
easy
A. Deletes the last element from the linked list
B. Adds a new element at the end of the linked list
C. Removes the first element from the linked list
D. Checks if the linked list is empty

Solution

  1. Step 1: Understand enqueue operation

    Enqueue means adding an element to the queue.
  2. Step 2: Enqueue in linked list context

    In a linked list queue, enqueue adds the new node at the end (tail).
  3. Final Answer:

    Adds a new element at the end of the linked list -> Option B
  4. Quick Check:

    Enqueue = Add at end [OK]
Hint: Enqueue always adds at the tail of the linked list [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Adding element at the head instead of tail
  • Thinking enqueue removes elements
  • Assuming enqueue checks emptiness
2. Which of the following is the correct syntax to create a new node and enqueue it in Go using a linked list?
easy
A. node := &Node{data: value, next: nil} queue.tail.next = node queue.tail = node
B. node := Node{value, nil} queue.head = node
C. node := &Node{value} queue.head.next = node
D. node := Node{data: value} queue.tail = node.next

Solution

  1. Step 1: Create new node correctly

    Use pointer to Node with data and next set to nil.
  2. Step 2: Link new node at tail and update tail

    Set current tail's next to new node, then update tail pointer.
  3. Final Answer:

    node := &Node{data: value, next: nil} queue.tail.next = node queue.tail = node -> Option A
  4. Quick Check:

    New node pointer + update tail = A [OK]
Hint: Always use pointer and update tail's next and tail itself [OK]
Common Mistakes:
  • Not using pointer for new node
  • Assigning new node to head instead of tail
  • Forgetting to update tail pointer
  • Incorrect struct initialization syntax
3. What will be the state of the queue after executing the following Go code?
type Node struct {
    data int
    next *Node
}

type Queue struct {
    head *Node
    tail *Node
}

func (q *Queue) enqueue(value int) {
    node := &Node{data: value}
    if q.tail == nil {
        q.head = node
        q.tail = node
    } else {
        q.tail.next = node
        q.tail = node
    }
}

func main() {
    q := &Queue{}
    q.enqueue(10)
    q.enqueue(20)
    q.enqueue(30)
    // Print queue
    current := q.head
    for current != nil {
        fmt.Print(current.data, " -> ")
        current = current.next
    }
    fmt.Print("null")
}
medium
A. 30 -> 20 -> 10 -> null
B. null
C. 10 -> 30 -> 20 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Enqueue elements in order

    Elements 10, 20, 30 are added at the tail one by one.
  2. Step 2: Traverse from head to tail

    Traversal prints nodes in order: 10, then 20, then 30, then null.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option D
  4. Quick Check:

    Enqueue order preserved in linked list [OK]
Hint: Enqueue adds at tail, print from head to tail [OK]
Common Mistakes:
  • Assuming new elements added at head
  • Printing tail to head order
  • Forgetting to print 'null' at end
  • Confusing enqueue with dequeue output
4. Identify the error in the following Go enqueue function for a linked list queue and select the fix.
func (q *Queue) enqueue(value int) {
    node := &Node{data: value}
    if q.head == nil {
        q.head = node
    } else {
        q.tail.next = node
    }
}
medium
A. Missing update of q.tail to new node after adding
B. Should check q.tail == nil instead of q.head == nil
C. Node should be created without pointer
D. Should assign q.head.next = node instead of q.tail.next

Solution

  1. Step 1: Analyze enqueue logic

    Function sets head if empty, else links new node after tail.
  2. Step 2: Check tail pointer update

    Function does not update q.tail to new node, so tail remains old node.
  3. Final Answer:

    Missing update of q.tail to new node after adding -> Option A
  4. Quick Check:

    Update tail pointer after enqueue [OK]
Hint: Always update tail pointer after adding new node [OK]
Common Mistakes:
  • Not updating tail pointer after enqueue
  • Checking wrong pointer for empty queue
  • Creating node without pointer
  • Linking new node to head instead of tail
5. You want to implement a queue using a linked list in Go that supports enqueue and dequeue efficiently. Which of the following changes will improve the enqueue operation's time complexity from O(n) to O(1)?
hard
A. Add new nodes at the head instead of the tail
B. Use a doubly linked list instead of singly linked list
C. Maintain a tail pointer to the last node and update it on enqueue
D. Traverse the list to find the last node each time before enqueue

Solution

  1. Step 1: Understand enqueue time complexity

    Without tail pointer, enqueue requires traversal to last node, O(n).
  2. Step 2: Using tail pointer

    Maintaining tail pointer allows direct access to last node, making enqueue O(1).
  3. Final Answer:

    Maintain a tail pointer to the last node and update it on enqueue -> Option C
  4. Quick Check:

    Tail pointer reduces enqueue to O(1) [OK]
Hint: Keep tail pointer to avoid traversal on enqueue [OK]
Common Mistakes:
  • Thinking doubly linked list alone improves enqueue
  • Adding at head breaks queue order
  • Traversing list each time causes O(n) complexity
  • Ignoring tail pointer update
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard