7 kyu

Fun with lists: length

1,448 of 3,454janitormeir

Description:

Implement the method length, which accepts a linked list (head), and returns the length of the list.

For example: Given the list: 1 -> 2 -> 3 -> 4, length should return 4.

The linked list is defined as follows:

function Node(data, next = null) {
  this.data = data;
  this.next = next;
}
class Node {
  public Object data;
  public Node next;

  Node(T data, Node next) {
    this.data = data;
    this.next = next;
  }
  
  Node(T data) {
    this(data, null);
  }
}
class Node {
  public $data, $next;
  public function __construct($data, $next = NULL) {
    $this->data = $data;
    $this->next = $next;
  }
}
class Node:
    def __init__(self, data, next=None): 
        self.data = data
        self.next = next
template <class T>
class Node
{
public:
    T data;
    Node *next;
    Node(T x, Node *p = NULL) : data(x), next(p) {}
    Node() : next(NULL) {}
};
typedef struct node_t {
    void *data;
    struct node_t *next;
} Node;
typedef struct node_t {
    void *data;
    struct node_t *next;
} Node;

Note: the list may be null and can hold any type of value.

Good luck!

This kata is part of fun with lists series:

Lists
Fundamentals

Stats:

CreatedNov 5, 2016
PublishedNov 5, 2016
Warriors Trained7538
Total Skips317
Total Code Submissions9765
Total Times Completed3454
JavaScript Completions1448
Java Completions825
PHP Completions179
Python Completions914
C Completions127
C++ Completions160
Total Stars73
% of votes with a positive feedback rating91% of 615
Total "Very Satisfied" Votes526
Total "Somewhat Satisfied" Votes70
Total "Not Satisfied" Votes19
Total Rank Assessments8
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • janitormeir Avatar
  • donaldsebleung Avatar
  • Blind4Basics Avatar
  • clcraig Avatar
  • albertogcmr Avatar
Ad