Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Singly Linked List

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

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

In Snowflake-style coding interviews, a common data structure task is to reverse a singly linked list in place. Write a function that takes the head of a singly linked list and returns the head of the reversed list.

Formal Specification

  • Input: head, the first node of a singly linked list, or null for an empty list.
  • Output: The new head node after reversing the list.
  • Each node has two fields: val and next.
  • You must reverse the links between nodes rather than creating a new list of copied nodes.

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= Node.val <= 10^4
  • Each node contains val and next
  • Do not allocate a copied list for the main solution

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