I was thinking of a solution to this problem.
My input:
1. Have a tail pointer which points to the last node.
2. Once you know the last pointer you can easily add a new node next to it.
Void Insert(Node N)
{
if (head == null) // linked list is empty
{
head = N; tail = N; tail.Next = head;
}
else
{
Node temp = tail.Next; // since this is circular tail will point to head
Tail.Next = N;
N.Next = temp; // correct
tail = N;
}
}
Can any1 think of better solution without using tail pointer? Also as stated in problem without traversing? This is an interview question, just need some inputs to find the best solution.