6 kyu

Linked Lists - Length & Count

3,809 of 10,391JDeBolt

Description:

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
      NodeListLength(null) => 0
      NodeListLength(1 -> 2 -> 3 -> null) => 3
length nil # 0
length (1 -> 2 -> 3 -> nil) # 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
      NodeListCount(null, 1) => 0
      NodeListCount(1 -> 2 -> 2 -> 2 -> 2 -> 3 -> 3 -> null, 2) => 4
count nil 1 # 0
count (1 -> 2 -> 3 -> nil) 1 # 1
count (1 -> 1 -> 2 -> 1 -> 2) 2 # 2

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.

Linked Lists
Data Structures
Fundamentals

Stats:

CreatedAug 3, 2015
PublishedAug 3, 2015
Warriors Trained28331
Total Skips7171
Total Code Submissions27821
Total Times Completed10391
JavaScript Completions3809
Python Completions3619
Ruby Completions579
Swift Completions670
C# Completions470
C++ Completions1223
NASM Completions32
COBOL Completions5
C Completions353
λ Calculus Completions15
Total Stars471
% of votes with a positive feedback rating92% of 1220
Total "Very Satisfied" Votes1044
Total "Somewhat Satisfied" Votes158
Total "Not Satisfied" Votes18
Total Rank Assessments29
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • JDeBolt Avatar
  • CIS 122 Avatar
  • NaMe613 Avatar
  • kodejuice Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • Souzooka Avatar
  • Bubbler Avatar
  • monadius Avatar
  • grant-rez Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • uniapi Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
  • Kacarott Avatar
  • Just4FunCoder Avatar
Ad