Ctrl K

How Often Each Value Appears

Easy
You are given a singly linked list head of positive integers. For every **distinct** value in it, count how many times it occurs. Return a new list made of these counts, one node per distinct value. For example, the list 4 → 7 → 4 has two distinct values, so the answer has two nodes: 2 and 1. The nodes of the answer may come in any order.
Example 1
head=list_node([4, 7, 4, 4, 9, 7]) → [1, 2, 3]

4 appears 3 times, 7 twice and 9 once. Any other order of these counts, such as [3, 2, 1], is accepted too.

Example 2
head=list_node([5, 5, 5]) → [3]

There is a single distinct value, and it appears three times.

Example 3
head=list_node([8, 3, 8, 1]) → [1, 1, 2]
Constraints
  • The list has between 1 and 10⁵ nodes
  • 1 ≤ Node.val ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Candidate With Most Votes EasyNote From Clipped Letters EasyFirst One-Off Letter EasyValues One Apart EasyWords Heard Only Once Easy
loading Python…
Input
head=list_node([4, 7, 4, 4, 9, 7])
Expected
[1, 2, 3]

Run checks the first 3 tests in your browser. Submit checks all 56 tests on the server, hidden ones included.