Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Linked List Subrange

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

Your question is Reverse Linked List Subrange. 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

Etsy may represent a sequence of listing identifiers as a singly linked list while preparing a reordered view. Given the list head and two 1-indexed positions, reverse the nodes between those positions in place and return the new head.

Formal Specification

Implement reverse_between(head, left, right), where head is the head of a singly linked list whose nodes have a val field and a next field. left and right are 1-indexed positions, inclusive. Reverse the links for that contiguous segment without creating replacement nodes, and return the resulting list head. The input list must be modified in place.

The test harness represents a linked list as a Python array and converts it to linked nodes before calling your function. It converts the returned list back to an array for comparison.

Constraints

  • 1 <= n <= 10^5
  • 1 <= left <= right <= n
  • Node values are integers from -10^9 through 10^9
  • The input is a valid singly linked list
  • No replacement list nodes may be created

Function Signature

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