Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse a Linked List

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

Your question is Reverse 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

San Diego Staffing stores a sequence of candidate records in a singly linked list. Reverse the list in place and return the new head so the records appear in the opposite order.

Formal Specification

Implement reverse_linked_list(head), where head is either a ListNode representing the first node or None. Each node has a val field and a next field. Return the new head after reversing every next pointer. Do not create new list nodes.

For examples and test cases, linked lists are represented as arrays of node values. The evaluator converts the input array into a linked list before calling the function and converts the returned list back into an array.

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= node.val <= 10^4
  • The list may be empty.
  • Modify existing links in place.
  • 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