3063. Linked List Frequency
Description
Given the head of a linked list containing k distinct elements, return the head to a new linked list of length k. This new linked list should contain the frequency of each distinct element in the given linked list, in any order.
Examples
Example 1:
     Input: head = [1,1,2,1,2,3]
Output: [3,2,1]

Explanation: There are 3 distinct elements in the list. The frequency of 1 is 3, the frequency of 2 is 2, and the frequency of 3 is 1. Hence, we return 3 -> 2 -> 1.

Note that 1 -> 2 -> 3, 1 -> 3 -> 2, 2 -> 1 -> 3, 2 -> 3 -> 1, and 3 -> 1 -> 2 are also valid answers.
   
content_copy Use code with caution.
Example 2:
     Input: head = [1,1,2,2,2]
Output: [2,3]

Explanation: There are 2 distinct elements in the list. The frequency of 1 is 2 and the frequency of 2 is 3. Hence, we return 2 -> 3.
   
content_copy Use code with caution.
Example 3:
     Input: head = [6,5,4,3,2,1]
Output: [1,1,1,1,1,1]

Explanation: There are 6 distinct elements in the list. The frequency of each of them is 1. Hence, we return 1 -> 1 -> 1 -> 1 -> 1 -> 1.
   
content_copy Use code with caution.


Q- can you explain the problem using you own words ? 
So we are given a singly linked list of length “n” with “k” distinct elements 
→ you task task to return a list containing the frequency of each element in this list 
Q- what is the input/output format ? 
Input : head: ListNode 
Output : List[int] 

Q- can you go through an exemple ? 
1->1->2->1->2->3 

Initiate a hashmap first always one of the powerful data structure to solve frequency problems oky 
Then traverse the linked list by using pointer updating 
Two conditions with their actions here : 
1- if the current node value in the hashmap 
  —> we just need to increment it’s value  in the hashmap by one 
2- else 
—> extend the hashmap by adding a new key and initiate it’s value to one oky 

Return list of the hashmap values oky 

Going step by step with the example 
1->1->2->1->2->3 
Pointer point to node zero with value 1 
Not in the hashmap 
	hashmap {} → {1:1} 
Update it now 
Pointer point to node one with value 1 
1 in the hashmap  we just update 
	Hashmap{1:1} → {1:2} 
Update it now 
Pointer point to node 3 value 2 
2 not in the hashmap we just update 
	Hashmap{1:2} → {1:2,2:1} 
1 in the hashamp we just update 
	Hashmap{1:2,2:1}  → {1:3,2:1} 
Update it now 
2 in the hashampa we just update 
	Hashmap{1:3,2:1} → {1:3,2:2} 
Update it now 
3 not  the hahmap we just update it 
	Hashmap{1:3,2:2} → {1:3,2:2,3:1} 


Finally get the list of values from the hashmap 
Values =[3,2,1] 

And return it 

Let's walk through an example:
Consider the linked list: 1 -> 1 -> 2 -> 1 -> 2 -> 3
One of the most effective data structures for solving frequency problems is a hashmap. We'll start by initializing an empty hashmap. Then, we'll traverse the linked list using a pointer, updating the hashmap as we go.
Here are the two conditions we'll encounter and their corresponding actions:
If the current node's value is already in the hashmap: Increment its corresponding value in the hashmap by one.
Otherwise (the current node's value is not in the hashmap): Add the current node's value as a new key to the hashmap and initialize its value to one.
Finally, we'll return a list of the hashmap's values.
Step-by-step with the example:
1 -> 1 -> 2 -> 1 -> 2 -> 3
Pointer points to the first node (value 1).
1 is not in the hashmap.
Hashmap: {} -> {1: 1}


1 -> 2 -> 1 -> 2 -> 3
Pointer points to the second node (value 1).
1 is in the hashmap.
Hashmap: {1: 1} -> {1: 2}


2 -> 1 -> 2 -> 3
Pointer points to the third node (value 2).
2 is not in the hashmap.
Hashmap: {1: 2} -> {1: 2, 2: 1}


1 -> 2 -> 3
Pointer points to the fourth node (value 1).
1 is in the hashmap.
Hashmap: {1: 2, 2: 1} -> {1: 3, 2: 1}


2 -> 3
Pointer points to the fifth node (value 2).
2 is in the hashmap.
Hashmap: {1: 3, 2: 1} -> {1: 3, 2: 2}


3
Pointer points to the sixth node (value 3).
3 is not in the hashmap.
Hashmap: {1: 3, 2: 2} -> {1: 3, 2: 2, 3: 1}


Finally, we extract the values from the hashmap: values = [3, 2, 1]
And return this list.
Q- do i approach the problem with the right direction ?