We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Ok I was confused by this too and literally when I woke up this morning I understood how it works. Here is a version of the answer written in Python that should be easier to understand.
Get Node Value
You are viewing a single comment's thread. Return to all comments →
Ok I was confused by this too and literally when I woke up this morning I understood how it works. Here is a version of the answer written in Python that should be easier to understand.
The mental image that came to me is dragging a rope of a certain length. (Remember, this was a dream.)
In a simple example, suppose we have the list
1 2 3 4 5
and we want to get position 3 from the end.As we advance to each new node, we incremenent the
len
value.When
len
is finally greater than theposition
value, that's the length of our rope. In this case, the length of the rope is 3 (3 nodes).As
head
keeps advancing through the list, we start "dragging the rope" by advancingtrailingNode
through the list behindhead
.Now that
len
is greater than the position, we start advancingtrailingNode
through the same list, but at 3 node positions behindhead
.Finally, when
head
reaches the end of the listtrailingNode
is now at the position of list length minus position.