We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2c002d2 commit 8c7400fCopy full SHA for 8c7400f
Medium/Linked List Frequency.java
@@ -0,0 +1,25 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+class Solution {
12
+ public ListNode frequenciesOfElements(ListNode head) {
13
+ Map<Integer, Integer> map = new HashMap<>();
14
+ for (ListNode curr = head; curr != null; curr = curr.next) {
15
+ map.put(curr.val, map.getOrDefault(curr.val, 0) + 1);
16
+ }
17
+ ListNode dummy = new ListNode(-1);
18
+ ListNode curr = dummy;
19
+ for (Integer key : map.keySet()) {
20
+ curr.next = new ListNode(map.get(key));
21
+ curr = curr.next;
22
23
+ return dummy.next;
24
25
+}
0 commit comments