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
- Linked List Manipulation Patterns pattern guide
- In-place Reversal subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Reverse Linked List
Other problems that use the same In-place Reversal and Linked List Manipulation Patterns techniques:
- Palindrome Linked List — Easy, same subpattern
- Remove Duplicates from Sorted List — Easy, same subpattern
- Remove Duplicates from Sorted List II — Medium, same subpattern
- Reverse Linked List II — Medium, same subpattern
- Reverse Nodes in k-Group — Hard, same subpattern
- Add Two Numbers — Medium, same pattern
Preparing your workspace
Progress is saved as you study.