educative.io

Why is second solution considered faster?

Why is the two pointers method faster?
Doesnt seem faster to me, they seem to have the same speed

If you iterate it once but do double the work in each iteration (moving two pointers instead of one), why would it be faster?

@Lucas2
Thanks for reaching out!
In LinkedList we do not have an index. To find the middle of the LinkedList we have two approaches
Approach # 1 Get the length of LinkedList and divide the length by 2 and move at that point with length/2 times iteration. In this approach, we have to iterate the list two times one for finding the length and the other for reaching the middle of the node, and remember one thing each iteration cause CPU Cycles.
Approach #2 In this approach we use two pointers fast and slow. Fast pointer moves two times and slow pointer moves 1 times in each iteration when fast pointer reaches at the end slow pointer at in the middle. In this approach, we have to iterate the list only one time and do not use a two-loop, and save CPU cycles.
so second approach consider as faster because less CPU cycles are consumed