Reverse Linked List

Reverse Linked List is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Linked List Manipulation Patterns pattern, in the In-place Reversal subpattern.

Problem statement

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

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:

- The number of nodes in the list is the range [0, 5000].

- -5000 <= Node.val <= 5000

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

Topics and companies

Topics: Linked List, Recursion.

Reported in interviews at Adobe, Amazon, Facebook, Apple, Microsoft, Qualcomm, Yandex, Google, Uber, Oracle, Bloomberg, IBM and others.

How to practise Reverse Linked List on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Reverse Linked List in the code editor, or read the Reverse Linked List editorial for a worked solution with its approach and complexity analysis.

Where Reverse Linked List sits in the DSA pattern sheet

Problems related to Reverse Linked List

Other problems that use the same In-place Reversal and Linked List Manipulation Patterns techniques:

Preparing your workspace

Progress is saved as you study.