Skip to content

Instantly share code, notes, and snippets.

@piecyk
Created May 13, 2014 10:51
Show Gist options
  • Select an option

  • Save piecyk/5ce3b3aabc215022df3d to your computer and use it in GitHub Desktop.

Select an option

Save piecyk/5ce3b3aabc215022df3d to your computer and use it in GitHub Desktop.
// task1
function solution(A) {
var pos = 0;
var len = A[pos] !== -1 ? 1 : 0;
// if -1 last node
while(A[pos] !== -1 && A[pos] !== undefined){
len++;
pos = A[pos];
}
return len;
}
console.log(solution([-1, -1, -1, 3, 2]));//0
console.log(solution([1, 4, -1, 3, 2])); // 4
console.log(solution([1, 4, 3, -1, 2])); // 5
console.log(solution([1, 4, 3])); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment