Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
it depends upon the no of nodes in the linkedlist. If it is even the logic will work. But what if the node is odd?
@debnath Let's say there are 3 nodes then the second node from initial state will be the middle one which the above code will return and in case #nodes are even for eg. 4 then the middle element can be 2 or 3 and the code will return 3th node which is indeed correct.
it is all nice but circulare list could also start in head and never come back to it:
1>2>3>4>5>3....
your solution only cover private case of fully circular list.
I think circular list means tail->next=head;
your example is just a list with loop in it, not circular list.
Anyway, you can ask the interviewer what he means by circular, this kind of question shouldn't matter.
have two pointer faster and slower, increase one node for a slower and two nodes for faster pointer and see when faster pointer reach the starting node again, slower pointer will point to the middle element.
That's a correct solution, however it's cleaner to do this in two steps:
First, find the length of the list (let's call it N) by iterating over it until we reach the starting node.
Now, move forward from the starting node N/2 steps. Done.
This is better than using two pointers, because you only need one pointer, and the number of forward movements is exactly the same. The difference is that it's done in two independent steps, rather than doing the full iteration and the half iteration at the same time, which is unneccessary and a lot more confusing.
Here is a fool proof way that will take O(n).
1.) make a temp value and set it equal to head.next
2.) iterate temp through linked list while incrementing a count
3.) if temp = head break out of loop. else increment count and set temp to temp.next
4.) divide count by 2. this is how many nodes away from the head node the mid node is
5.) iterate from the head node to the mid node and return the mid node
I think we use logic of slow and fast pointer here.
code is as follow :
node * middle(node *head)
{
node *sp=NULL;
node *fp=NULL;
sp=head;
fp=head;
while(sp)
{
sp=sp->next;
fp=fp->next;
if(fp->next!=NULL)
fp=fp->next;
if(fp->data==head->data)
return sp;
}
return NULL;
}
This is pretty close.
1) You should check the pointer fp -> next with head (before the 2nd next),
2) What if the list is odd length, what if its even. You took care of the odd case. If it's even, even though there's no "middle", the loop will go infinite
.
This is very close, just need the following
1. After each increment of fast pointer check if head is reached, in case list has odd number of items
2. Instead of comparing data of fast pointer to the head of data pointer, compare the fast pointer itself with the head pointer. This is in case the list has two items with the same data.
Since it may not be guaranteed the list is indeed circular, extra cautious checks are done:
typedef struct SCList
{
int Data;
struct SCList *Next;
} TSCList, *PSCList;
PSCList FindMedian(PSCList head)
{
bool fStart = false;
PSCList median = NULL;
PSCList fast = NULL;
if (head)
{
fStart = true;
median = head;
fast = head;
while (fast && (fast->Next != head && fast != head || fStart))
{
fStart = false;
if (fast->Next && (fast->Next->Next != head && fast->Next != head))
{
fast = fast->Next->Next;
median = median->Next;
}
else
{
fast = fast->Next;
}
}
}
return median;
}
Have to pointer objects and traverse in opposite directions.
After each step, check if the both objects point to same location.
When both objects point to same location, that is the mid element.
@Sarvan, I think your answer is really good. But just want some psuedo code.
Could you pls write some pseudo code?
sajain is right. In a regular linked list you can't move in reverse direction.
have 2 pointers fast and slow. fast pointer moves in speed of (speed of slowpointer*3). the 2 pointer will meet in the middle element.
Assuming that by mid element you mean a node which is equidistant from the initial node whether you see from right or left i.e.
-1->2->3->4->1-
here 3 is middle as it is equidistant from head, which points to 1, from left and right.
Algorithm :
1. Take two pointers and point them to the initial node (head).
2. Move one pointer (turtle) by one step, another pointer(rabbit) by two step.
3. if rabbit == head OR rabbit->next == head then return turtle as the mid element.
Code :
- Ashish May 18, 2013