Midpoint chain: First qualifying slip
Stuck or curious how this works? Visualize Fast & Slow Pointers ↗
Inputs
head is the first node of a linked list. In examples, [4, 7] means nodes 4 then 7, linked in that order. [] means no nodes. The runner builds the nodes for you.
Your tasks
- Create a list
middleValues. Find the middle node of the linked list given byhead. Each node stores a number in val and a link in next. Use two pointers: one moves one link at a time, and the other moves two. If there are two middle nodes, choose the second one. Record the values from that middle node to the end. An empty list gives an emptymiddleValues. - Read
middleValuesfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
midpointChainFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If head is [-7, 2, 2, 2, 0] and k is 4, then middleValues is [2, 2, 0] and midpointChainFirstQualifyingSlip returns -1.
Rules
- If no number is at least
k, return -1. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: head is [0, 5, 18, 18, -3] and k is 5.
TEST DATA 2: head is [8, 8, -5, 8] and k is 6.
Examples
Input: head = [0,5,18,18,-3] k = 5 Output: 18
middleValues is [18, 18, -3]. Here k is 5. The first number at least 5 is 18. Return that value.
Input: head = [8,8,-5,8] k = 6 Output: 8
middleValues is [-5, 8]. Here k is 6. The first number at least 6 is 8. Return that value.
Input: head = [] k = 2 Output: -1
middleValues is []. Here k is 2. No number is at least 2, so return -1.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- Linked lists do not loop back to an earlier node. Tree examples list nodes by level; null means a missing child. The runner builds the nodes shown in the examples.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n); extra memory, including the helper list, O(n). n is the number of nodes. These labels describe how work and memory grow with the input.
Practice focus
Build middleValues first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.