Skip to content

Commit 665b45c

Browse files
authored
Create linked-list-frequency.py
1 parent 5581c91 commit 665b45c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/linked-list-frequency.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# linked list
5+
class Solution(object):
6+
def frequenciesOfElements(self, head):
7+
"""
8+
:type head: Optional[ListNode]
9+
:rtype: Optional[ListNode]
10+
"""
11+
curr = dummy = ListNode(0)
12+
cnt = 0
13+
while head:
14+
cnt += 1
15+
if not head.next or head.next.val != head.val:
16+
curr.next = ListNode(cnt)
17+
curr = curr.next
18+
cnt = 0
19+
head = head.next
20+
return dummy.next

0 commit comments

Comments
 (0)