3. You want to implement a method peek() for a linked list queue that returns the front element without removing it. Which is the correct implementation?
easy
A. return this.head ? this.head.value : null;
B. return this.tail ? this.tail.value : null;
C. return this.head.next ? this.head.next.value : null;
D. return this.tail.next ? this.tail.next.value : null;
Solution
Step 1: Understand peek operation in queue
Peek returns front element without removing it, which is at head in linked list queue.
Step 2: Check correct pointer for front element
Head points to front node; tail points to end node. So return head.value if head exists.
Final Answer:
return this.head ? this.head.value : null; -> Option A
Quick Check:
Peek returns head value [OK]
Hint: Peek returns head value without dequeue [OK]
Common Mistakes:
Returning tail value instead of head
Accessing next of tail which is always null
Returning head.next value which skips front
4. Consider this JavaScript code for a linked list queue: