Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

In-Place Reverse With Edge Cases

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

Your question is In-Place Reverse With Edge Cases. 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

Vectra AI may process linked sequences of security alerts while preserving the existing node allocation. Given the head of a singly linked list, reverse the list in place and return the new head.

You must modify each node's next pointer rather than creating replacement nodes. The input is represented in examples and test cases as an array of node values, but the function receives a ListNode object. The evaluator converts the serialized values into linked nodes before calling the function and serializes the returned list afterward.

Formal Specification

Implement reverse_linked_list(head):

  • Input: head, either None or a reference to the first ListNode. Each node contains val and next.
  • Output: The new head of the reversed linked list.
  • The list contains no cycles.
  • All original nodes must be reused.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The linked list contains no cycles
  • Do not allocate replacement list nodes
  • Use O(1) auxiliary space

Function Signature

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