There are two obvious ways to structure a linked list in Mathematica, "left":
{1, {2, {3, {4, {5, {6, {7, {}}}}}}}}
And "right":
{{{{{{{{}, 7}, 6}, 5}, 4}, 3}, 2}, 1}
These can be made with:
toLeftLL = Fold[{#2, #} &, {}, Reverse@#] & ;
toRightLL = Fold[List, {}, Reverse@#] & ;
If I use these, and do a simple ReplaceRepeated
to walk through the linked list, I get drastically different Timing
results:
r = Range[15000];
left = toLeftLL@r;
right = toRightLL@r;
Timing[i = 0; left //. {head_, tail_} :> (i++; tail); i]
Timing[i = 0; right //. {tail_, head_} :> (i++; tail); i]
(* Out[6]= {0.016, 15000} *)
(* Out[7]= {5.437, 15000} *)
Why?