Assuming there is an array A = [1,3,-1,4,2]
, We have the linked list built as following,
A[0] = 1
A[1] = 3
A[3] = 4
A[4] = 2
A[2] = -1
We have to ignore when the next poiner is -1
, which means the linked list ends now. So, the length of this list is 4
as it ends on A[2]
Write an programm to calculate a length of the given array of length N where N could be between (1..200000) as per the above explanation.
You could choose Ruby/Python/Javascript for this.