Implement a Singly Linked List

Data Structures
Easy
Amazon
55.5K views

Implement a complete Singly Linked List with methods for `addAtHead`, `addAtTail`, `deleteAtIndex`, and `get` operations. Focus on pointer manipulation.

Why Interviewers Ask This

Amazon interviewers ask this to evaluate your mastery of pointer manipulation and memory management fundamentals. They specifically test if you can handle edge cases like empty lists or single-node scenarios without crashing. This question reveals your ability to think algorithmically about data traversal and your capacity to write clean, bug-free code under pressure, a core competency for Amazon's engineering standards.

How to Answer This Question

1. Clarify Requirements: Immediately confirm if the list should support duplicates, if indices are zero-based, and how to handle invalid delete requests. 2. Define Structure: Verbally define your Node class with value and next pointers before writing any logic. 3. Maintain State: Explicitly declare head and tail pointers in your class initialization to optimize tail operations. 4. Implement Core Logic: Start with addAtHead as it is simplest, then move to addAtTail, ensuring you update both head and tail references correctly. 5. Handle Edge Cases: For deleteAtIndex, carefully manage the case where deleting the head node requires updating the head reference, and ensure you don't dereference null pointers during traversal. 6. Verify Complexity: Conclude by stating time complexity (O(n) for get/delete, O(1) for add) and space complexity (O(1)).

Key Points to Cover

  • Explicitly handling the 'empty list' edge case in every method
  • Correctly updating both head and tail pointers simultaneously when adding to the end
  • Preventing null pointer exceptions during traversal before accessing properties
  • Maintaining O(1) time complexity for add operations by tracking tail
  • Demonstrating clear variable naming and logical flow for pointer reassignment

Sample Answer

I will implement a Singly Linked List using a class structure with an inner Node definition. First, I'll initialize the constructor to set both head and tail pointers to null, which allows us to handle empty list scenarios efficiently. For addAtHead, I create a new node, point its next to the current head, and update the head. If the list was empty, the new node also becomes the tail. For addAtTail, I traverse to the end using the tail pointer to append in O(1) time, updating the previous tail's next pointer and refreshing the tail reference. The get method requires traversing from the head to the target index, checking bounds at each step to prevent null pointer exceptions. For deleteAtIndex, I must handle three specific cases: deleting the head (updating the head reference), deleting the tail (updating the tail reference), or deleting a middle node. In all cases, I will use a prev pointer to maintain the link integrity before bypassing the target node. I will prioritize clarity and safety checks over micro-optimizations to ensure the code is robust against edge cases like negative indices or out-of-bounds deletions.

Common Mistakes to Avoid

  • Failing to update the tail pointer when adding a new element to the end of the list
  • Dereferencing a null pointer while traversing to find the index without proper boundary checks
  • Forgetting to handle the specific scenario where the list contains only one node during deletion
  • Not returning the correct boolean status or throwing appropriate errors for invalid indices

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 73 Amazon questions