Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Linked List and Test

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

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

Google Chrome may represent a sequence of navigation records as a singly linked list. Implement reverse_linked_list to reverse the list in place and return the new head. Also design tests that verify pointer order, boundary cases, and that the original nodes are reused rather than copied.

Formal Specification

The input is head, either None or a reference to a ListNode object. Each ListNode has an integer value field and a next field containing another ListNode or None. Return the head of the reversed list. Do not allocate replacement list nodes, and do not modify node values.

For the JSON test cases below, each input array represents node values in traversal order. The test harness converts the array to linked nodes before calling the function and serializes the returned list back to an array.

Constraints

  • 0 <= number of nodes <= 10^5
  • -10^9 <= node.value <= 10^9
  • The list is finite and acyclic
  • Node objects must be reused rather than copied

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