27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> 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 />

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 />

pg 50<br />

If we move two pointers, one with speed 1 <strong>and</strong> 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, <strong>and</strong> 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) <strong>and</strong> 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 <strong>and</strong> 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 <strong>and</strong> n2 is k nodes from LoopStart (as shown above).<br />

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

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

1 0 9<br />

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

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

Saved successfully!

Ooh no, something went wrong!