Solution #1: Double Iteration #
why is called double interaction if we iterate over the list 1 time
Solution #2: Two Pointers #
why is more efficient approach if both solutions have the same space and time complexity?
why is called double interaction if we iterate over the list 1 time
why is more efficient approach if both solutions have the same space and time complexity?
Hi @Josimar_Amilcar_Fern
Thanks for reaching out!
The solution #1 is called double iteration because we iterate the list two times one for finding length for this we have to iterate the list to the end node and the other loop is to reach the nth node. For the calculating length, we have to consume CPU cycles in that sense we call this approach less efficient as compared to the second approach.
In Solution #2: we used two-pointer (nth_node, and end_node) nth_node This iterator will reach the Nth node and end_node this iterator will reach the end of the list. 1. Once end_node
is at nth position from the start, move both end_node
and nth_node
pointers simultaneously. 1. When end_node
reaches the end, nth_node
is at the Nth position from the end. This approach helps to reduce CPU cycles so we call this approach more efficient as compare to the first once