395. Reverse Nodes in K-Group (No Extra Space)

HardLinked ListLinked ListPointers

Given a linked list, reverse the nodes k at a time and return the modified list, using only O(1) extra space. Nodes in a final group of fewer than k are left in their original order. The list is given as an array; return the result as an array. Input: '[list], k'.

Input: '[list], k'.

Output: Array — the modified list.

Examples

Example 1
Input: [1,2,3,4,5], 2
Output: [2,1,4,3,5]
Explanation: Full pairs reversed; last node unchanged.
Example 2
Input: [1,2,3,4,5], 3
Output: [3,2,1,4,5]
Explanation: First triple reversed.
Example 3
Input: [1], 1
Output: [1]
Explanation: Single node.

Constraints

Asked by

MicrosoftAmazonBloombergGoogleMetaInfosys
Solve this problem in the editor →