Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Reverse a Linked List

Easy Acceptance 78.1% Points 20

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.

Constraints
  • 1 <= number of nodes, data of nodes <= 10^5
Hints
  • Expected Time Complexity: O(n)
  • Expected Space Complexity: O(1)
Topics
Linked List
Companies
Amazon Microsoft Samsung Walmart Paypal Zoho
Added 02 Dec 2024 78.1% acceptance
Ln 1, Col 1Sign in to save your code
10 20 30 40 50
50 40 30 20 10