Middle Element of a Linked List
Updated
Problem statement
You are given head, the first node of a singly linked list. Return the value stored in the middle node of the list.
Number the nodes 1, 2, ..., n starting from head.
- If
nis odd, the middle node is node(n + 1) / 2. - If
nis even, there are two middle nodes,n / 2andn / 2 + 1. Return the value of the second one, noden / 2 + 1.
The middle is about position in the list, not about the size of the values. The values can be in any order.
Function
middleValue(head: ListNode) → intExamples
Example 1
head = [4,9,2,7,5]return = 2The list has 5 nodes, an odd number. The middle node is node (5 + 1) / 2 = 3, which holds 2.
Example 2
head = [3,8,1,6]return = 1The list has 4 nodes, an even number, so there are two middle nodes: node 2, which holds 8, and node 3, which holds 1. Return the value of the second one, 1.
Example 3
head = [-7]return = -7A list with one node has that node as its middle, so the answer is -7.
Constraints
1 <= n <= 1000, wherenis the number of nodes.- Every node value is between
-10^5and10^5. - Values may repeat.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
- Maximum Supplier ProfitONSITE INTERVIEW · Seen Aug 2024
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
- Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse Linked ListThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Detect a Linked-List CycleGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect a Cycle in a Linked ListPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions