Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse a Custom Linked List

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

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

Blurb's preview pipeline stores ordered page-preview records in a custom singly linked list. Implement reverse_linked_list(head) to reverse the list in place and return the new head.

Each node is provided by the interviewer and has two fields: value, containing any page-preview record, and next, containing another node or None. Do not create replacement nodes or use an auxiliary collection.

Formal Specification

  • Input: head, either a Node object representing the first node of a singly linked list or None.
  • Output: The Node object that was previously the tail, now serving as the new head. Return None for an empty list.
  • The function must preserve every node and reverse each next pointer.

Constraints

  • 0 <= number of nodes <= 10^5
  • Each node has fields value and next
  • The list is singly linked and acyclic
  • value may be any Python object
  • 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