Chain inspection: Positive-ink invoice
Stuck or curious how this works? Visualize Tree Inorder Traversal ↗
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
values.headis the first node of a linked list. A node holds a number in val and a link to the next node in next. Follow next until there are no more nodes. Record each val in order. The list has no loops. An empty list gives an emptyvalues. - Add only the numbers in
valuesthat are greater than 0. Store the sum inpositiveTotal. - Complete
chainInspectionPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If head is [8, 11, 9, -2, 9], then values is [8, 11, 9, -2, 9] and chainInspectionPositiveInkInvoice returns 37.
Rules
- Skip 0 and negative numbers.
- If no number is greater than 0, return 0.
Test data
TEST DATA 1: head is [12, 0, -3, 7, 12].
TEST DATA 2: head is [5, 5, -8, 5].
Examples
Input: head = [12,0,-3,7,12] Output: 31
values is [12, 0, -3, 7, 12]. Keep [12, 7, 12] and add those numbers. Return 31.
Input: head = [5,5,-8,5] Output: 15
values is [5, 5, -8, 5]. Keep [5, 5, 5] and add those numbers. Return 15.
Input: head = [] Output: 0
values is []. Keep [] and add those numbers. Return 0.
Input limits
- 0 <= input size <= 64.
- 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 values 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.