How to know weather linked list is circular or not?
Respostas da entrevista
Sigiloso
10 de jun. de 2016
Write a query that can update student result form Pass to fail and fail to pass
11
Sigiloso
25 de mar. de 2019
Use 2 pointer approach, one pointer starting from 2 nodes ahead, if the two pointers meet at some point, list is circular
6
Sigiloso
23 de jul. de 2019
No
Sigiloso
23 de jul. de 2019
No
Sigiloso
18 de out. de 2019
A linked list is called circular if it is not NULL-terminated and all nodes are connected in the form of a cycle. or an empty linked list is a circular.
example inn java
static boolean isCircular( Node head)
{
if (head == null) //empty linked list
return true;
Node node = head.next; //next head
while (node != null && node != head)
node = node.next;
return (node == head);//loop stopped because of circular
}
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}
public static void main(String args[])
{
Node head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(3);
head.next.next.next = newNode(4);
System.out.print(isCircular(head)? "Yes\n" :
"No\n" );
// Making linked list circular
head.next.next.next.next = head;
System.out.print(isCircular(head)? "Yes\n" :
"No\n" );
}