Hello guys, Today I am sharing a live experience of mine in tally.
First of all I was asked about circular linked list.
Then a series of bombarding questions from the same concept.
First question was :-
How to check whether a linked list is circular or not. I gave the answer like this :-
Take 2 pointers FIRST and SECOND, Initially pointing at the same node.then traverse the linked list FIRST jumping 1 node at a time and SECOND jumping two nodes at a time. We have to check until both point to same location.
i.e
while(FIRST!=SECOND)
{
FIRST=FIRST->NEXT;
SECOND=(SECOND->NEXT)->NEXT;
}
Then second question asked to me was :-
Can you do it using the single pointer?
I thought for a while and gave a clear answer that it is not possible.
If you think you can do it using one pointer, then do share your code and explanation too.
First of all I was asked about circular linked list.
Then a series of bombarding questions from the same concept.
First question was :-
How to check whether a linked list is circular or not. I gave the answer like this :-
Take 2 pointers FIRST and SECOND, Initially pointing at the same node.then traverse the linked list FIRST jumping 1 node at a time and SECOND jumping two nodes at a time. We have to check until both point to same location.
i.e
while(FIRST!=SECOND)
{
FIRST=FIRST->NEXT;
SECOND=(SECOND->NEXT)->NEXT;
}
Then second question asked to me was :-
Can you do it using the single pointer?
I thought for a while and gave a clear answer that it is not possible.
If you think you can do it using one pointer, then do share your code and explanation too.
There is a solution with single pointer:
->Take the starting address of a pointer into a variable,say head=PTR;
->move the pointer PTR=PTR->next;
->if(PTR==head && PTR->next!=NULL)
pf("ITS CIRCULAR");
elseif(PTR->next==NULL)
printf("its not circular");
else
PTR=PTR->next;
It is wrong solution. for eg.
3->4->5->6->7->9
let our PTR is at 3.
here PTR==HEAD and PTR->next!=NULL
condition true but still it is not a circular linked list.