Skip to main content
Notice removed Reward existing answer by Bubbler
Bounty Ended with user's answer chosen by Bubbler
Notice added Reward existing answer by Bubbler
Bounty Started worth 300 reputation by Bubbler
Notice removed Reward existing answer by rydwolf
Bounty Ended with d01's answer chosen by rydwolf
Notice added Reward existing answer by rydwolf
Bounty Started worth 100 reputation by rydwolf
edited tags
Link
Tweeted twitter.com/StackCodeGolf/status/1338453731461959680
Became Hot Network Question
Source Link
rydwolf
  • 19.3k
  • 2
  • 90
  • 180

Follow a linked list

For this challenge, a linked list looks like this:

[1, 2, 4, 0, 6, 1, 3, 1] 

You'll notice there's no data; each item in the list is just a reference to the index of the next one. Your task is to write a program or function which will follow a linked list like this forever, outputting each item as it is encountered. For example, for the above input:

[1, 2, 4, 6, 3, 0, 1, ...] 

Input and output can be represented in any reasonable manner. All numbers in the input will be valid indices in the list, and you can choose if it's 0- or 1-indexed. The output will be infinite, and as long as your program would theoretically give a correct result given infinite time and resources it's valid.

Test cases:

Zero indexed, using lists.

[0] -> [0, 0, ...] [1, 0] -> [1, 0, 1, ...] [0, 1, 2] -> [0, 0, ...] [2, 1, 0] -> [2, 0, 2, ...] [4] -> (does not need to be handled)