Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List Midpoint

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

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

Tempus Labs processes ordered records represented as singly linked lists. Given the head of a singly linked list, return the value stored in its midpoint node.

Use the second middle node when the list has an even number of nodes. For example, the midpoint of 1 -> 2 -> 3 -> 4 is 3.

Formal Specification

Implement midpoint(head), where head is either None or a ListNode with:

  • val, an integer value
  • next, a reference to the next ListNode or None

Return the integer value of the midpoint node. The input list is non-empty. Do not modify the list, and do not convert it into another data structure.

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^9 <= node.val <= 10^9
  • The list contains no cycles
  • The input list must remain unchanged

Function Signature

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