Q. How do you count the number of elements in the circular linked list? (Solved)

1. public int length(node head){int length = 0;if( head == null)return 0;node temp = head.getnext();while(temp != head){temp = temp.getnext();length++;}return length;}

2. public int length(node head){int length = 0;if( head == null)return 0;node temp = head.getnext();while(temp != null){temp = temp.getnext();length++;}return length;}

3. public int length(node head){int length = 0;if( head == null)return 0;node temp = head.getnext();while(temp != head && temp != null){temp = head.getnext();length++;}return length;}

4. public int length(node head){int length = 0;if( head == null)return 0;node temp = heagetnext();while(temp != head && temp == null){temp = heagetnext();length++;}return length;}

  • a. public int length(node head){int length = 0;if( head == null)return 0;node temp = head.getnext();while(temp != head){temp = temp.getnext();length++;}return length;}
Subscribe Now

Get All Updates & News