Why increase pointer by two while finding loop in linked list, why not 3,4,5?

From a correctness perspective, there is no reason that you need to use the number two. Any choice of step size will work (except for one, of course). However, choosing a step of size two maximizes efficiency. To see this, let’s take a look at why Floyd’s algorithm works in the first place. The idea … Read more

Sorting a linked list

Functional Quicksort and Mergesort Here’s a linked list with quicksort and mergesort methods written in a functional style: class List { public int item; public List rest; public List(int item, List rest) { this.item = item; this.rest = rest; } // helper methods for quicksort public static List Append(List xs, List ys) { if (xs … Read more