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 & BuildOneTwoThreearrow-up-right Linked Lists - Length & Countarrow-up-right Linked Lists - Get Nth Nodearrow-up-right Linked Lists - Insert Nth Nodearrow-up-right Linked Lists - Sorted Insertarrow-up-right Linked Lists - Insert Sortarrow-up-right Linked Lists - Appendarrow-up-right Linked Lists - Remove Duplicatesarrow-up-right Linked Lists - Move Nodearrow-up-right Linked Lists - Move Node In-placearrow-up-right Linked Lists - Alternating Splitarrow-up-right Linked Lists - Front Back Splitarrow-up-right Linked Lists - Shuffle Mergearrow-up-right Linked Lists - Sorted Mergearrow-up-right Linked Lists - Merge Sortarrow-up-right Linked Lists - Sorted Intersectarrow-up-right Linked Lists - Iterative Reversearrow-up-right Linked Lists - Recursive Reversearrow-up-right

Inspired by Stanford Professor Nick Parlante's excellent Linked List teachings.arrow-up-right

Solutions

🧠 C++

Last updated

Was this helpful?