Bird
Raised Fist0
DSA Goprogramming~5 mins

Enqueue Using Linked List in DSA Go - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Enqueue Using Linked List
O(1)
Understanding Time Complexity

We want to understand how the time to add an item to a queue using a linked list changes as the queue grows.

How does the number of steps grow when we add more items?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Node structure
 type Node struct {
     value int
     next  *Node
 }

// Queue with front and rear
 type Queue struct {
     front *Node
     rear  *Node
 }

// Enqueue adds a new node at the rear
 func (q *Queue) Enqueue(val int) {
     newNode := &Node{value: val}
     if q.rear == nil {
         q.front = newNode
         q.rear = newNode
         return
     }
     q.rear.next = newNode
     q.rear = newNode
 }
    

This code adds a new item to the end of a queue implemented with a linked list.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Adding a new node by updating pointers at the rear.
  • How many times: This happens once per enqueue call, no loops or recursion inside.
How Execution Grows With Input

Each enqueue adds one node by changing a few pointers, no matter how big the queue is.

Input Size (n)Approx. Operations
10About 1 pointer update
100About 1 pointer update
1000About 1 pointer update

Pattern observation: The work stays the same no matter how many items are in the queue.

Final Time Complexity

Time Complexity: O(1)

This means adding an item takes the same small amount of time no matter how big the queue is.

Common Mistake

[X] Wrong: "Enqueue takes longer as the queue grows because we have to find the end each time."

[OK] Correct: We keep a pointer to the rear node, so we add directly without searching, keeping time constant.

Interview Connect

Knowing that enqueue on a linked list queue is constant time shows you understand efficient data structure design, a key skill in coding interviews.

Self-Check

"What if we did not keep a rear pointer and only had a front pointer? How would the time complexity of enqueue change?"

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