Skip to content

Practice Reverse Nodes in Even Length Groups

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2074
LeetCode Problem

Reverse Nodes in Even Length Groups

Problem Statement

You are given the head of a linked list. The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words, Note that the length of the last group may be less than or equal to 1 + the length of the second to last group. Reverse the nodes in each group with an even length, and return the head of the modified linked list.

Source: Reverse Nodes in Even Length Groups on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [5,2,6,3,9,1,7,3,8,4]
Output
[5,6,2,3,9,1,4,8,3,7]

# Example 2

Input
head = [1,1,0,6]
Output
[1,0,1,6]

# Example 3

Input
head = [1,1,0,6,5]
Output
[1,0,1,5,6]

Constraints

The number of nodes in the list is in the range [1, 10^5].
0 <= Node.val <= 10^5

Related LeetCode Problems

Practice more problems similar to Reverse Nodes in Even Length Groups.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.