7 kyu

Fun with lists: indexOf

1,016 of 2,008janitormeir

Description:

Implement the method indexOf (index_of in PHP), which accepts a linked list (head) and a value, and returns the index (zero based) of the first occurrence of that value if exists, or -1 otherwise.

For example: Given the list: 1 -> 2 -> 3 -> 3, and the value 3, indexOf / index_of should return 2.

The linked list is defined as follows:

struct Node {
    struct Node *next;
    int data;
};
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

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 4, 2016
PublishedNov 4, 2016
Warriors Trained3895
Total Skips186
Total Code Submissions7745
Total Times Completed2008
JavaScript Completions1016
Java Completions628
PHP Completions136
C Completions106
Python Completions234
Total Stars41
% of votes with a positive feedback rating93% of 410
Total "Very Satisfied" Votes365
Total "Somewhat Satisfied" Votes36
Total "Not Satisfied" Votes9
Total Rank Assessments8
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • janitormeir Avatar
  • donaldsebleung Avatar
  • albertogcmr Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
  • Kacarott Avatar
Ad