Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Linked List in Groups

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reverse Linked List in Groups. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

AutoRABIT Holding processes ordered deployment records represented as a singly linked list. Given the list head and an integer k, reverse the nodes in every consecutive group of k nodes and return the new head.

If fewer than k nodes remain at the end, leave that final group unchanged. Nodes must be rearranged in place, without creating replacement nodes or copying values.

Formal Specification

Implement reverse_k_group(head, k), where head is either None or the head of a singly linked ListNode with fields val and next, and k is a positive integer. Return the head of the transformed linked list. Test inputs serialize linked lists as arrays, and the evaluator converts them to ListNode objects before calling the function. Outputs are serialized back to arrays.

Constraints

  • 0 <= n <= 10^5, where n is the number of nodes
  • 1 <= k <= 10^5
  • -10^9 <= node.val <= 10^9
  • The input is a singly linked list
  • Only existing nodes may be rearranged

Function Signature

def reverse_k_group(head, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output