Sol:
We can solve this easily using Hare and Tortoise approach. Basically have 2 pointers both pointing to the start of the list. Increment one pointer by 1 and
another by 2. When pointer 2 reaches end of the list pointer 1 will be in the middle of the linked list.
Node *FindMiddle( Node *head )
{
Node *p1, p2;
p1 = p2 = head;
while( p2 && p2->next )
{
p2 = p2->next->next;
p1 = p1->next;
}
return( p1 );
}
Deleting all your perforce workspaces
12 years ago