Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Reverse a Linked List

Easy Acceptance 78.10% Points 20.00

Given the head of a linked list, the task is to reverse this list and return the reversed head.

Examples
Example 1

Example 1

Input: Linked list: 10 -> 20 -> 30 -> 40 -> 50

Output: 50 -> 40 -> 30 -> 20 -> 10

Explanation: The linked list is reversed such that the last node becomes the head, and the head becomes the last node.

Example 2

Example 2

Input: Linked list: -3 -> -2 -> -1 -> 0 -> 1

Output: 1 -> 0 -> -1 -> -2 -> -3

Explanation: The linked list is reversed while maintaining the relative positions of negative and positive numbers.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= number of nodes, data of nodes <= 10^5
Companies
Amazon Microsoft Samsung Walmart Paypal + 1 more
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions