25.03.2013 Views

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solutions to Chapter 2 | Linked Lists<br />

2 5 Given a circular linked list, implement an algorithm which returns node at <strong>the</strong> beginning<br />

of <strong>the</strong> loop<br />

1 0 9<br />

DEFINITION<br />

Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an<br />

earlier node, so as to make a loop in <strong>the</strong> linked list<br />

EXAMPLE<br />

Input: A -> B -> C -> D -> E -> C [<strong>the</strong> same C as earlier]<br />

Output: C<br />

SOLUTION<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Data Structures<br />

pg 50<br />

If we move two pointers, one with speed 1 and ano<strong>the</strong>r with speed 2, <strong>the</strong>y will end up meeting<br />

if <strong>the</strong> linked list has a loop Why? Think about two cars driving on a track—<strong>the</strong> faster car<br />

will always pass <strong>the</strong> slower one!<br />

The tricky part here is finding <strong>the</strong> start of <strong>the</strong> loop Imagine, as an analogy, two people racing<br />

around a track, one running twice as fast as <strong>the</strong> o<strong>the</strong>r If <strong>the</strong>y start off at <strong>the</strong> same place,<br />

when will <strong>the</strong>y next meet? They will next meet at <strong>the</strong> start of <strong>the</strong> next lap<br />

Now, let’s suppose Fast Runner had a head start of k meters on an n step lap When will<br />

<strong>the</strong>y next meet? They will meet k meters before <strong>the</strong> start of <strong>the</strong> next lap (Why? Fast Runner<br />

would have made k + 2(n - k) steps, including its head start, and Slow Runner would have<br />

made n - k steps Both will be k steps before <strong>the</strong> start of <strong>the</strong> loop )<br />

Now, going back to <strong>the</strong> problem, when Fast Runner (n2) and Slow Runner (n1) are moving<br />

around our circular linked list, n2 will have a head start on <strong>the</strong> loop when n1 enters Specifically,<br />

it will have a head start of k, where k is <strong>the</strong> number of nodes before <strong>the</strong> loop Since n2<br />

has a head start of k nodes, n1 and n2 will meet k nodes before <strong>the</strong> start of <strong>the</strong> loop<br />

So, we now know <strong>the</strong> following:<br />

1 Head is k nodes from LoopStart (by definition)<br />

2 MeetingPoint for n1 and n2 is k nodes from LoopStart (as shown above)<br />

Thus, if we move n1 back to Head and keep n2 at MeetingPoint, and move <strong>the</strong>m both at <strong>the</strong><br />

same pace, <strong>the</strong>y will meet at LoopStart

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!