Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List and Binary Tree Ops

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

Your question is Linked List and Binary Tree Ops. 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

The Zeta mobile app stores a user's activity feed as a singly linked list. Given the head of the list, reverse the list in place and return the new head so the feed displays the newest node first.

Formal Specification

Implement reverse_list(head):

  • Input: head, either None or a reference to the first ListNode in a singly linked list. Each node has integer field val and pointer field next.
  • Output: The new head after reversing every next pointer. The original nodes must be reused, and no new list nodes may be created.
  • The test representation uses an array of values. For example, [1, 2, 3] represents 1 -> 2 -> 3.

Constraints

  • 0 <= number of nodes <= 10^5
  • -10^9 <= node.val <= 10^9
  • The list contains no cycles
  • No new list nodes may be created

Function Signature

def reverse_list(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output