Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List or Binary Tree Implementation

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

Your question is Linked List or Binary Tree Implementation. 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

Andela Talent Cloud stores ordered workflow steps as singly linked lists. Implement reverse_between to reverse the nodes from position left through position right, inclusive, without creating replacement nodes.

Formal Specification

The function receives head, the first ListNode in a singly linked list, and two 1-based positions, left and right. Return the new head after reversing that segment. Each node has an integer val field and a next field containing another node or None. The list must be modified in place, and node identity outside the reversed segment must remain unchanged.

For test cases, linked lists are represented as JSON arrays. The evaluation harness converts the array to linked nodes before calling the function and converts the returned list back to an array.

Constraints

  • 0 <= len(head) <= 500
  • 1 <= left <= right <= len(head) when the list is non-empty
  • -10^4 <= node.val <= 10^4
  • The input positions are valid
  • The list must be modified in place

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