JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: Check if given Linked List is a Circular Linked List or not.

 Problem Description: Write a program to check given singly linked list is a circular linked list or not. Circular linked list is a sequence of elements in which every element has link to its next element in the sequence and the last element has a link to the first element in the sequence. Algorithm: Traverse from head node. Compare the current node with its next node. If the data of the next node is same as current nodes data, then delete the next node. Update current nodes next pointer to next nodes next value.

 IsCircularLinkedList ```package com.java2novice.algos; public class IsCircularLinkedList { static class Node { int data; Node next; Node(int tmp) {data = tmp;} } static boolean isCircularLinkedList(Node head) { if(head == null) { return Boolean.FALSE; } Node temp = head; do { temp = temp.next; } while(temp != null && temp != head); return (temp == head); } public static void main(String[] a){ Node n1 = new Node(34); Node n2 = new Node(25); Node n3 = new Node(31); Node n4 = new Node(56); Node n5 = new Node(45); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n1; System.out.println("Is it circular linked list: "+isCircularLinkedList(n1)); } } ```

 Output: `Is it circular linked list: true`

List Of All Interview Programs:

Java2Novice - YouTube Channel
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.
-- William Arthur Ward

Other Interesting Sites

 Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle. Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.