Singly Linked Lists
# CHAPTER 8
Singly Linked Lists
1. Introduction
In the previous chapter, we established the theoretical architecture of a Linked List: independent Nodes connected by a single, forward-facing memory pointer. Because the pointers only go in one direction (Head to Tail), this specific structure is formally known as a Singly Linked List. In this chapter, we transition from theory to heavy algorithmic programming. Manipulating pointers is notoriously difficult and error-prone. A single pointer pointing to the wrong object will detach the entire data chain and crash the software. We will master the core algorithms: Traversal, Insertion, Deletion, and the legendary Reversal.2. Learning Objectives
By the end of this chapter, you will be able to:-
Write a
whileloop to traverse a Singly Linked List.
- Execute pointer manipulation to insert new Nodes at the Head, Tail, and Middle.
- Securely delete a Node without breaking the memory chain.
- Solve the "Reverse a Linked List" FAANG interview problem.
3. Traversal (Printing the List)
You cannot use a standardfor(i = 0; i < n; i++) loop on a linked list because there are no indexes! You must use a while loop that follows the pointers.
Time Complexity: O(n). We must visit every single node once.
4. Insertion Algorithms
Inserting data requires creating a new Node and carefully rewiring the pointers. The order of operations is critical. Always attach the new Node to the list BEFORE you break the old links!#### 4.1 Inserting at the Front (The Head) This is the superpower of Linked Lists. It is instantaneously O(1).
#### 4.2 Inserting at the End (The Tail)
To insert at the end, you must traverse the entire list to find the final node. This takes O(n) time!
*(Note: If you maintain a separate Tail pointer variable in your class, this drops to O(1)!)*
5. Deletion Algorithms
Deleting a node means rewiring the pointers to "skip over" the target node. Once nothing points to the target node, the Garbage Collector will delete it from RAM.Deleting a specific value (e.g., delete the node with data '20'):
- 1. Traverse the list to find the node *immediately before* the target.
- 2. Change the previous node's pointer to point to the node *after* the target.
- 3. The target is now completely disconnected.
6. The Ultimate Interview: Reversing a Linked List
This is arguably the most famous software engineering interview question. You must flip all the arrows backward in O(n) time and O(1) space.[1] -> [2] -> [3] -> NULL becomes NULL <- [1] <- [2] <- [3]
You need three pointers: prev, current, and next.
7. Complexity Analysis
| Operation | Best Case | Worst Case |
|---|---|---|
| Search | O(1) | O(n) |
| Insert at Head | O(1) | O(1) |
| Insert at Tail | O(n) | O(n) *(O(1) if Tail pointer exists)* |
| Delete at Head | O(1) | O(1) |
| Delete specific node | O(1) | O(n) (Requires search) |
8. Common Mistakes
-
Null Pointer Exceptions: The most common error in pointer logic. If you try to execute
current.nextbutcurrentis alreadyNULL, the program will instantly crash. Always checkif (current != null)before accessing properties!
-
Breaking the Chain during Insertion: If you are inserting
BbetweenAandC, and you changeA.next = Bfirst, you have lost the memory address ofCforever. You must doB.next = Cfirst, and thenA.next = B. Order matters absolutely.
9. Best Practices
- Use a Dummy Node: When solving complex linked list algorithms (like merging two lists), creating a temporary "Dummy" head node drastically simplifies the code and prevents edge-case crashes when dealing with empty lists.
10. Exercises
- 1. Write a function to find the exact middle node of a Linked List in a single O(n) pass. *(Hint: Use a Slow Pointer and a Fast Pointer that moves twice as fast).*
-
2.
Walk through the Reversal algorithm on paper with three nodes. Track the
prev,curr, andnextvariables at each step.
11. MCQs with Answers
When traversing a Singly Linked List, what is the mandatory mathematical condition required to safely terminate the while loop?
When inserting a brand new Node directly into the middle of a Linked List, what is the critical order of pointer reassignment required to prevent catastrophic data loss?
What is the standard Time Complexity for inserting a Node at the absolute end (Tail) of a Singly Linked List that only tracks the Head pointer?
How can a software architect modify the base Linked List class to reduce Tail Insertion time to O(1)?
In the standard algorithm to Reverse a Singly Linked List, what is the explicit architectural purpose of the temporary next_node variable?
To safely delete a specific Node from the middle of the list, what pointer modification must occur?
What happens if a developer attempts to execute current.next on a Node when current is actually NULL?
Why is searching for a specific integer in a Singly Linked List strictly an O(n) operation?
When utilizing the "Fast and Slow Pointer" technique (Floyd's Algorithm), the Fast pointer moves 2 steps while the Slow pointer moves 1 step. When the Fast pointer hits NULL, where is the Slow pointer?
What is the Space Complexity of reversing a Linked List in-place using pointers?
13. Interview Preparation
Top Interview Questions:- *Algorithmic Coding:* "Write an algorithm to detect a 'Cycle' in a Linked List. (A Cycle occurs if the Tail node maliciously points back to a node in the middle of the list, creating an infinite loop)." *(Answer: Use Floyd's Tortoise and Hare algorithm. If the Fast pointer ever equals the Slow pointer, a cycle exists!).*
- *Algorithmic Coding:* "Write an O(n) algorithm to merge two sorted Linked Lists into a single, perfectly sorted Linked List without creating any new Nodes."
Common Pitfalls:
- In whiteboard interviews, constantly talking about updating pointers but forgetting to actually return the *new Head* at the very end of the function! If the calling program doesn't get the new Head, the data is useless.