Write a function that returns the value of the linked-list node when given the linked-list and the count from the right.
Linked-list:
- 3
- 5
- 6
- 2
- 7
- 5
Example Cases
-
Test:
(list, 3)Expected:2 -
Test:
(list, 1)Expected:5 -
Test:
(list, 0)Expected:3
Possible Solutions
Using 2 Passes
Idea: In first pass, count total of linked-list nodes. Subtract the second argument from the total (position). In the second pass, go to the node that equals position and return list.value.
Using 1 Pass
Idea: Track the count and make another value equal to the second argument (position). Traverse through the linked-list using list.next() and through each iteration update list.next() to list.next().next().