Reverse Nodes in K Group

Hard

Problem: Reverse Nodes in K Group

Reverse every k nodes of a given linked list, where k is a positive integer.

Problem Statement

Given the head of a linked list and an integer k, reverse every k nodes of the linked list in-place. The nodes from index i to i+k-1 should form a reversed group.

The original list should still be accessible and usable during the process, with all elements correctly linked after reversing every k nodes. This modification does not change the overall length of the list.

Rules and Constraints

  • k is a positive integer, k >= 1.
  • The elements in the linked list do not contain duplicate values.
  • The nodes in the linked list are uniquely identifiable by their values.
  • Time complexity: O(n) where n is the total number of nodes in the linked list.
  • Space complexity: O(1), which implies that we only use a constant amount of space during the operation.

Note: This problem can be solved in-place, without modifying the original list or creating a new linked list.

Example

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

CompaniesGoogle
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.