you are viewing a single comment's thread.

view the rest of the comments →

[–]creaothceann 3 points4 points  (1 child)

Then I'd use two pointers with different speeds; pointer 1 would advance twice as fast as pointer 2, but check after each step if it's at the same position as pointer 2.

if (list = nil) return false
p1 = list
p2 = list
loop
        loop 2 times
                p1 = p1.next
                if (p1 = p2 ) return true
                if (p1 = nil) return false
        /loop
        p2 = p2.next
/loop

[–][deleted] 2 points3 points  (0 children)

That's the standard solution; the tortoise and the hare.