206. Reverse Linked List
easyAsked at Juniper NetworksReverse a singly linked list in place. Juniper networking OS code uses linked lists to chain packet buffers and routing table entries; being able to reverse a list cleanly in O(1) space is a basic pointer-manipulation skill every Juniper SWE candidate is expected to demonstrate.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Juniper Networks loops.
- Glassdoor (2026-Q1)— Mentioned in Juniper SWE phone-screen reports as a standard linked-list warm-up question.
- Blind (2025-12)— Juniper candidates report Reverse Linked List as a common first coding question in technical screens.
Problem
Given the head of a singly linked list, reverse the list, and return the reversed list's head.
Constraints
The number of nodes in the list is in the range [0, 5000].−5000 <= Node.val <= 5000.
Examples
Example 1
head = [1,2,3,4,5][5,4,3,2,1]Explanation: All pointers flipped in sequence.
Example 2
head = [1,2][2,1]Explanation: Two-node list reversed.
Example 3
head = [][]Explanation: Empty list returns null.
Approaches
1. Iterative — O(1) space
Walk the list with three pointers (prev, curr, next). At each step, redirect curr.next to prev, then advance all three.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null;
let curr = head;
while (curr !== null) {
const next = curr.next; // save next before overwriting
curr.next = prev; // reverse pointer
prev = curr; // advance prev
curr = next; // advance curr
}
return prev; // prev is the new head
}Tradeoff: O(n) time, O(1) space. Preferred at Juniper — in-place mutation mirrors how packet buffer chains are managed in kernel-level networking code.
2. Recursive
Recursively reverse the tail, then make the last node point back to the current head.
- Time
- O(n)
- Space
- O(n) call stack
function reverseList(head) {
if (head === null || head.next === null) return head;
const newHead = reverseList(head.next);
head.next.next = head; // make next node point back
head.next = null; // cut old forward pointer
return newHead;
}Tradeoff: Elegant but uses O(n) stack space. Juniper interviewers will usually follow up asking for the iterative version to avoid stack overflow on very long packet chains.
Juniper Networks-specific tips
Start by drawing the pointer rearrangement on the whiteboard or talking through it step by step — Juniper values methodical rigor over speed. Mention that in production networking code this operation runs on buffer descriptor rings where saving and restoring the next pointer before overwriting it is critical to not dropping packets. Offer the iterative solution as your primary answer.
Common mistakes
- Not saving curr.next before overwriting curr.next — you lose the rest of the list.
- Returning curr instead of prev at the end — after the loop, curr is null; prev is the new head.
- Forgetting to handle the null or single-node edge case in the recursive version.
- Setting head.next = null before wiring head.next.next = head in the recursive version — causes null dereference.
Follow-up questions
An interviewer at Juniper Networks may pivot to one of these next:
- Reverse Linked List II (LC 92) — reverse only nodes from position left to right.
- How would you reverse a doubly linked list?
- Reverse Nodes in k-Group (LC 25) — reverse chunks of k nodes at a time.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why do we return prev instead of curr after the iterative loop?
When the loop exits, curr is null (we moved past the old tail). prev is pointing at the old tail, which is now the new head.
What is the space complexity of the recursive solution?
O(n) due to the call stack depth. For a 5000-node list that is 5000 stack frames — acceptable in most environments but avoidable with the iterative approach.
Can I use an array to reverse the list?
Yes, but it uses O(n) extra space. Mention it as a naive option, then explain that the in-place iterative approach is preferred for memory-constrained networking contexts.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Juniper Networks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →