Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List or Bit Counting

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

Your question is Linked List or Bit Counting. 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

Diagnostic pipelines in Texas Instruments Code Composer Studio may process records stored as singly linked lists. Given the head of a singly linked list and an integer k, reverse the list in consecutive groups of k nodes. If the final group contains fewer than k nodes, leave that group unchanged.

Formal Specification

Implement reverse_in_groups(head, k), where head is either a ListNode object or None, and k is a positive integer. Each node has an integer val field and a next pointer. Return the head of the modified list. The reversal must be performed by changing pointers, not by creating replacement nodes or converting the entire list to an array.

For testing, linked lists are serialized as arrays. The evaluator converts each input array into linked nodes before calling the function and converts the returned list back into an array.

Constraints

  • 0 <= number of nodes <= 10^5
  • 1 <= k <= 10^5
  • Node values are integers in the range [-10^9, 10^9]
  • The input list is singly linked
  • The solution must reuse existing nodes

Function Signature

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