31. Reverse Linked List

31. Reverse Linked List

Easy
63.0%
2.1k
linked-listrecursiontwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale backend system processes streams of events using linked data structures for efficient insertions and deletions. In some workflows such as rollback operations, cache reordering, or pipeline rewiring, the system needs to reverse the order of elements in a singly linked list.

Each node in the list contains a value and a reference to the next node.
The system requires an in-place solution that efficiently reverses the list without allocating extra memory.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given the head of a singly linked list, reverse the list and return the new head.

You must reverse the list in place.

Input Format

  • head: the head node of a singly linked list

Each node is defined as:

ListNode {
int val;
ListNode next;
}

Output Format

  • Return the head of the reversed linked list

Examples

Example 1:

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2:

Input: head = [1,2]
Output: [2,1]

Example 3:

Input: head = []
Output: []

Constraints

  • 0 <= number of nodes <= 5,000
  • -10^9 <= Node.val <= 10^9
  • The list can be empty

Loading editor...

[1,2,3,4,5]
[5,4,3,2,1]

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback