给定一个单向链表,任务是计算数据值等于其频率的节点数。
例子:
Input: Linked list = 2 -> 3 -> 3 -> 3 -> 4 -> 2
Output: 2
Frequency of element 2 is 2
Frequency of element 3 is 3
Frequency of element 4 is 1
So, 2 and 3 are elements which have same frequency as it’s value
Input: Linked list = 1 -> 2 -> 3 -> 4 -> 5 -> 6
Output: 1
推荐:在继续解决方案之前,请先在{IDE}上尝试您的方法。
方法:
解决这个问题的方法如下
- 遍历链表并使用映射存储数组中每个元素的频率
- 遍历地图并计算频率等于其值的元素数量
下面是上述方法的实现:
C++
/* Link list node */
#include
using namespace std;
class Node {
public:
int data;
Node* next;
};
// Function to add a node at the
// beginning of List
void push(Node** head_ref, int data)
{
/* allocate node */
Node* new_node =new Node();
/* put in the data */
new_node->data = data;
// link the old list off the new
// node
new_node->next = (*head_ref);
// move the head to point to the
// new node
(*head_ref) = new_node;
}
// Counts the no. of occurences of a
// node in a linked list
int countValuesWithSameFreq(Node* start)
{
map mpp;
Node* current = start;
int count = 0;
while (current != NULL) {
mpp[current->data] += 1;
current = current->next;
}
int ans = 0;
for (auto x : mpp) {
int value = x.first;
int freq = x.second;
// Check if value equls to frequency
// and increment the count
if (value == freq) {
ans++;
}
}
return ans;
}
// main program
int main()
{
Node* head = NULL;
push(&head, 3);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 2);
push(&head, 3);
cout << countValuesWithSameFreq(head);
return 0;
}
Java
/* Link list node */
import java.util.*;
class GFG{
public static class Node
{
int data;
Node next;
};
// Function to add a node at the
// beginning of List
static Node push(Node head_ref, int data)
{
// Allocate node
Node new_node = new Node();
// Put in the data
new_node.data = data;
// Link the old list off the new
// node
new_node.next = head_ref;
// Move the head to point to the
// new node
head_ref = new_node;
return head_ref;
}
// Counts the no. of occurences of a
// node in a linked list
static int countValuesWithSameFreq(Node start)
{
HashMap mpp = new HashMap<>();
Node current = start;
while (current != null)
{
if (mpp.containsKey(current.data))
{
mpp.put(current.data,
mpp.get(current.data) + 1);
}
else
{
mpp.put(current.data, 1);
}
current = current.next;
}
int ans = 0;
for(Map.Entry x : mpp.entrySet())
{
int value = x.getKey();
int freq = x.getValue();
// Check if value equls to frequency
// and increment the count
if (value == freq)
{
ans++;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
Node head = null;
head = push(head, 3);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
System.out.print(countValuesWithSameFreq(head));
}
}
// This code is contributed by amal kumar choubey
Python3
# Link list node
class Node:
def __init(self, next):
self.data = 0
self.next = None
# Function to add a node at the
# beginning of List
def push(head_ref, data):
# Allocate node
new_node = Node()
# Put in the data
new_node.data = data
# Link the old list off the new
# node
new_node.next = (head_ref)
# Move the head to point to the
# new node
(head_ref) = new_node
return head_ref
# Counts the no. of occurences of a
# node in a linked list
def countValuesWithSameFreq(start):
mpp = dict()
current = start
count = 0
while (current != None):
if current.data not in mpp:
mpp[current.data] = 0
mpp[current.data] += 1
current = current.next
ans = 0
for x in mpp.keys():
value = x
freq = mpp[x]
# Check if value equls to frequency
# and increment the count
if (value == freq):
ans += 1
return ans
# Driver code
if __name__=='__main__':
head = None
head = push(head, 3)
head = push(head, 4)
head = push(head, 3)
head = push(head, 2)
head = push(head, 2)
head = push(head, 3)
print(countValuesWithSameFreq(head))
# This code is contributed by rutvik_56
C#
/* Link list node */
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
// Function to add a node at the
// beginning of List
static Node push(Node head_ref, int data)
{
// Allocate node
Node new_node = new Node();
// Put in the data
new_node.data = data;
// Link the old list off the new
// node
new_node.next = head_ref;
// Move the head to point to the
// new node
head_ref = new_node;
return head_ref;
}
// Counts the no. of occurences of a
// node in a linked list
static int countValuesWithSameFreq(Node start)
{
Dictionary mpp = new Dictionary();
Node current = start;
while (current != null)
{
if (mpp.ContainsKey(current.data))
{
mpp[current.data] = mpp[current.data] + 1;
}
else
{
mpp.Add(current.data, 1);
}
current = current.next;
}
int ans = 0;
foreach(KeyValuePair x in mpp)
{
int value = x.Key;
int freq = x.Value;
// Check if value equls to frequency
// and increment the count
if (value == freq)
{
ans++;
}
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
Node head = null;
head = push(head, 3);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
Console.Write(countValuesWithSameFreq(head));
}
}
// This code is contributed by Rohit_ranjan
输出:
2
复杂度分析:
时间复杂度:对于给定的大小为n 的链表,我们迭代它一次。所以这个解的时间复杂度是O(n)
空间复杂度:对于给定大小为n 的链表,我们使用了一个额外的映射,该映射最多可以有 n 个键值,因此该解决方案的空间复杂度为O(n)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live