Adobe Interview Question
Software Engineer / DevelopersIs it a C code, or C++ code.
My question is how to create a looped linked list. This part of code is not clear??
To find where the node where the loop happens:
Take 2 pointers A B. A goes 1 node each time. B goes 2 nodes each time. If they meet there is a loop.
Now make B stop at where they meet. Restart A. This time both of them go 1 node at a time. When they meet again it is where the loop happens. Just make the next pointer NULL.
No, this is not correct... I checked with an example given above, 1->2->3->4->5->6->7->4->5->6->7.
And it didn't gave the correct result, of finding the loop starting node.
It does work. In your example they meet at 4. This is a solution from the Cracking Interview book. You can better understand the solution if you read the whole explanation there.
Node n1 = n2 = head;
while (true) {
n1 = n1.next;
n2 = n2.next.next;
if (n1 == n2)
break;
}
n1 = head;
while (n1.next != n2.next) {
n1 = n1.next;
n2 = n2.next;
}
n2.next=null
This one doesn't seems to be complete, as when the linked list is not looped, this code will fail.
Hi Sathya,
I think second while should be like
Node* Prev = NULL;
while (n1.next != n2.next) {
Prev = n2;
n1 = n1.next;
n2 = n2.next;
}
Prev.next=null
Hi Tulley
Are u sure abt it cos I m getting correct answers for my code.Take this for eg
1-->2-->3-->4-->5
|_______|
(3,4,5 is the loop)
now both ptrs n1,n2 start at 1
n1 points to 2,n2 points to 3
n1 points to 3,n2 points to 5
n1 points to 4,n2 points to 4
now n1 starts at 1,n2 at 4
n1 points to 2,n2 points to 5
both next are same so loop breaks and n2.next set to null
let me know if any thing wrong with this
bool LoopCorrectn(nodeptr ptr)
{
nodeptr slow = ptr;
nodeptr fast = ptr;
nodeptr head = ptr;
nodeptr head2=NULL;
do
{
if(slow)
slow = slow->link;
else
return 1;
if(fast->link)
fast =fast->link->link;
else
{
return 1;
}
}while(slow!=fast);
slow=slow->link;
fast->link=NULL;
head2 = slow;
int len1 =0;
while(head)
{
len1++;
head=head->link;
}
int len2=0;
while(head2)
{
len2++;
head2=head2->link;
}
head=ptr;
head2=slow;
if(len1<=len2)
{
if(len1<len2)
{
for(int dlen =len2-len1;dlen>0;dlen--)
head2=head2->link;
}
}
else
{
for(int dlen =len1-len2;dlen>0;dlen--)
head=head->link;
}
while(head->link!=head2->link)
{
head = head->link;
head2 = head2->link;
}
head2->link=NULL;
fast->link=slow;
}
It's not clear how the linked list is implemented. Single linked list or double linked list. Suppose single linked list:
1) head->elem1->elem2->elem3->head
2) head->elem1->elem2->elem3->tail->head
pp1 described above.
pp2:
if (tail != null && tail.next != null && tail.next = head){
tail.next = null;
}
Pseudo code:
1. Two pointers start from head
2. One pointer advances one while the other advances two.
3.1 If two pointers meet, there is a loop
3.2 If pointer reachs the end, there is no loop
4. Find out how many nodes in the loop, say k.
5. Reset one pointer to the head, and the other pointer to head + k.
6. Move both pointers at the same pace, they will meet at loop starting node
7. Move one pointer till its next node is the loop starting node. It's the loop ending node
8. Set the next node of the loop ending node to fix the loop
9. Print the list
Output:
- leeym March 07, 2011$ ./remove-loop-from-linked-list
There are 4 nodes in the loop of the list.
node 4 is the loop starting node.
node 7 is the loop ending node.
The list without loop is: 1 2 3 4 5 6 7