Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List Inversion

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

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

A Graviton Research Capital processing stream stores ordered events in a singly linked list. Reverse the list in place so the events appear in the opposite order, then return the new head so the resulting sequence can be printed.

Formal Specification

Each node has an integer val field and a next field pointing to the next node or None. Implement reverse_linked_list(head), where head is the first ListNode or None. Return the head of the reversed list. The caller will print the returned list from left to right. Do not create new list nodes, and do not convert the list into an array.

For test cases, linked lists are represented as arrays for serialization. For example, [1, 2, 3] represents 1 -> 2 -> 3.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The list contains no cycles
  • No new list nodes may be created
  • 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