Linked Lists - Length & Count
Linked Lists - Length & Count
Implement Length() to count the number of nodes in a linked list.
length(null) => 0
length(1 -> 2 -> 3 -> null) => 3
Node.Length(nullptr) => 0
Node.Length(1 -> 2 -> 3 -> nullptr) => 3
length(null) => 0
length(1 -> 2 -> 3 -> null) => 3
Implement Count() to count the occurrences of an integer in a linked list.
count(null, 1) => 0
count(1 -> 2 -> 3 -> null, 1) => 1
count(1 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 3 -> 3 -> null, 2) => 4
Node.Count(null, value => value == 1) => 0
Node.Count(1 -> 3 -> 5 -> 6, value => value % 2 != 0) => 3
count(null, 1) => 0
count(1 -> 2 -> 3 -> nullptr, 1) => 1
count(1 -> 1 -> 1 -> 2 -> 2 -> 2 -> 2 -> 3 -> 3 -> nullptr, 2) => 4
I've decided to bundle these two functions within the same Kata since they are both very similar.
The push()
/Push()
and buildOneTwoThree()
/BuildOneTwoThree()
functions do not need to be redefined.
Related Kata in order of expected completion (increasing difficulty): Linked Lists - Push & BuildOneTwoThree Linked Lists - Length & Count Linked Lists - Get Nth Node Linked Lists - Insert Nth Node Linked Lists - Sorted Insert Linked Lists - Insert Sort Linked Lists - Append Linked Lists - Remove Duplicates Linked Lists - Move Node Linked Lists - Move Node In-place Linked Lists - Alternating Split Linked Lists - Front Back Split Linked Lists - Shuffle Merge Linked Lists - Sorted Merge Linked Lists - Merge Sort Linked Lists - Sorted Intersect Linked Lists - Iterative Reverse Linked Lists - Recursive Reverse
Inspired by Stanford Professor Nick Parlante's excellent Linked List teachings.
Solutions
🧠 C++
/* Node Definition
struct Node {
Node * next;
int data;
}
*/
int Length(Node *head)
{
int counter = 0;
for(;head!=nullptr;head=head->next)
++counter;
return counter;
}
int Count(Node *head, int data)
{
int counter = 0;
for(;head!=nullptr;head=head->next)
if(head->data==data) ++counter;
return counter;
}
Last updated
Was this helpful?