-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3063_Linked_List_Frequency.py
36 lines (30 loc) · 1 KB
/
3063_Linked_List_Frequency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def frequenciesOfElements(self, head: Optional[ListNode]) -> Optional[ListNode]:
# Freqeuncy dictionary
val_freq_dict = {}
# Loop - all element
while head:
# Get value of each node
val = head.val
# Update freqeuncy dictionary
if val in val_freq_dict:
val_freq_dict[val] += 1
else:
val_freq_dict[val] = 1
# Update head
head = head.next
# Construct new linked list
dummy = ListNode(val = -1)
head = dummy
# Loop - all frequency dictionary (freq only)
for freq in val_freq_dict.values():
new_node = ListNode(val = freq)
head.next = new_node
head = new_node
# RETURN - freqeuncy linked list
return dummy.next