Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reversing a Linked List

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

Your question is Reversing a 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

Judge Group's internal engineering tools use singly linked lists for ordered processing tasks. Implement a function that reverses a singly linked list in place and returns the new head.

Formal Specification

Each node has an integer value and a next pointer. The function receives head, a ListNode reference or None, and returns the new ListNode head after reversing every link. Do not create replacement nodes or use an array to store the values. The original nodes must be reused.

For examples and test cases, linked lists are represented as arrays from head to tail. The evaluator converts an input array into linked nodes before calling the function and serializes the returned list back into an array.

Constraints

  • 0 <= n <= 10^5, where n is the number of nodes
  • -10^9 <= node.value <= 10^9
  • The list is acyclic and singly linked
  • Reuse the existing nodes
  • Use O(1) extra 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