Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Middle of Linked List

Easy Acceptance 87.00% Points 20.00

You are given the head of a singly linked list. Your task is to find and return the middle node of the linked list. If the linked list contains an even number of nodes, return the second middle node.

Examples
Example 1

Example 1

Input: head = [1,2,3,4,5]

Output: 3

Explanation: The linked list contains 5 nodes. The middle node is 3.

Example 2

Example 2

Input: head = [1,2,3,4,5,6]

Output: 4

Explanation: The linked list contains 6 nodes. There are two middle nodes (3 and 4), so the second middle node, 4, is returned.

Example 3

Example 3

Input: head = [10]

Output: 10

Explanation: The linked list contains only one node, so it is the middle node.

Hints
Hint 1
N/A
Constraints
  • 1 = Number of nodes = 105
  • -10? = Node.data = 10?
Companies
Microsoft
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions