Reverse Linked List

Easy

Reverse Linked List

Problem Statement

Given the head of a singly linked list, write a function that reverses the linked list in-place without using any additional memory.

Rules and Constraints

  • The function should modify the original linked list.
  • The function should not use any additional data structures such as arrays, lists, or stacks.
  • The function should return the head of the reversed linked list.
  • The input linked list is a singly linked list, meaning each node only points to the next node in the list.
  • The input linked list may contain circular references and should be ignored during reversal.
  • The input linked list may have a cycle, and the function should terminate when it encounters a cycle.

Time and Space Complexity

  • The time complexity of the function should be O(n), where n is the number of nodes in the linked list.
  • The space complexity of the function should be O(1), excluding the space required for the input and output linked lists.

Example

Input: {"head":[1,2,3,4,5]} Output: [5,4,3,2,1]

CompaniesGoogleMeta
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.