Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Reverse Linked List in k Groups

XPChallenge Points: 30
levelLevel: Hard

Given the head of a linked list, reverse the nodes of the list in groups of size k and return the new head.

   k is a positive integer and k ≤ the length of the list.

   If the number of nodes at the end is less than k, leave them as they are.

   Only the node links may be changed, not the node values.

Example 1:

Input: head = [10,20,30,40,50,60], k = 3

Output: [30,20,10,60,50,40]

Explanation:

The first three nodes are reversed, then the next three.

Example 2:

Input: head = [5,6,7,8,9], k = 2

Output: [6,5,8,7,9]

Explanation:

Nodes are reversed in pairs; the last node remains as is.

Example 3:

Input: head = [1], k = 1

Output: [1]

Explanation:

A single node stays the same.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result