0% found this document useful (0 votes)
5 views6 pages

Circular Linked List Operations in Java

The document contains a Java program that implements a circular linked list with various functionalities including inserting nodes, finding the length, reversing the list, and checking if the list is a palindrome. It defines a ListNode class to represent each node in the list and includes methods for manipulating the circular linked list. The main method demonstrates these functionalities by reading input values, creating the list, and performing operations on it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Circular Linked List Operations in Java

The document contains a Java program that implements a circular linked list with various functionalities including inserting nodes, finding the length, reversing the list, and checking if the list is a palindrome. It defines a ListNode class to represent each node in the list and includes methods for manipulating the circular linked list. The main method demonstrates these functionalities by reading input values, creating the list, and performing operations on it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

public class Main {

static Scanner input = new Scanner([Link]);

public static void main(String[] args) {

int number1 = [Link]();

int number2 = [Link]();

int number3 = [Link]();

int number4 = [Link]();

int number5 = [Link]();

int number6 = [Link]();

int number7 = [Link]();

int number8 = [Link]();

int number9 = [Link]();

ListNode head = new ListNode(number1);

ListNode node1 = new ListNode(number2);

ListNode node2 = new ListNode(number3);

ListNode node3 = new ListNode(number4);

ListNode node4 = new ListNode(number5);

ListNode node5 = new ListNode(number6);

ListNode node6 = new ListNode(number7);

ListNode node7 = new ListNode(number8);

ListNode node8 = new ListNode(number9);

[Link] = node1;

[Link] = node2;

[Link] = node3;

[Link] = node4;

[Link] = node5;

[Link] = node6;
[Link] = node7;

[Link] = node8;

[Link] = head;

findlength(head);

insertAtBeginning(head);

insertSpecific(head, 7, 10);

boolean isPalindrome = isCircularListPalindrome(head);

[Link](isPalindrome);

head = reverse(head);

if (head != null) {

ListNode current = head;

do {

[Link]([Link] + " ");

current = [Link];

} while (current != head);

} else {

[Link]("The circular linked list is empty.");

public static void insertSpecific(ListNode head, int index, int item) {

int count = 0;

ListNode current = head;

ListNode temp = null;

while (index > ++count) {

current = [Link];

temp = [Link];

ListNode newNode = new ListNode(item);

[Link] = newNode;
[Link] = temp;

ListNode current1 = head;

do {

[Link]([Link] + " ");

current1 = [Link];

} while (current1 != head);

[Link]();

public static void findlength(ListNode head) {

ListNode current = head;

int count = 0;

do {

count++;

current = [Link];

} while (current != head);

[Link]("Length: " + count);

public static void insertAtBeginning(ListNode head) {

ListNode newHead = new ListNode(1);

[Link] = head; // Link the new head node to the original list

ListNode current = head;

do {

if ([Link] == head) {

[Link] = newHead;

break; // Break the loop after updating the last node's next pointer

current = [Link];

} while (current != head);


ListNode current1 = newHead; // Start from the newHead to print the circular linked list

do {

[Link]([Link] + " ");

current1 = [Link];

} while (current1 != newHead);

public static ListNode reverse(ListNode head){

ListNode current = head;

ListNode prev = null;

do{

ListNode temp = [Link];

[Link] = prev;

prev = current;

current = temp;

}while(current != head);

head = prev;

return prev;

public static boolean isCircularListPalindrome(ListNode head) {

if (head == null || [Link] == head) {

// An empty or single-node circular list is considered a palindrome.

return true;

// Find the length of the circular list

int length = 0;

ListNode current = head;

do {

length++;

current = [Link];
} while (current != head);

// Find the middle node

int mid = length / 2;

ListNode middle = head;

for (int i = 0; i < mid; i++) {

middle = [Link];

// Reverse the second half

ListNode secondHalf = reverse(middle);

// Compare the first and reversed second halves

ListNode firstHalf = head;

for (int i = 0; i < mid; i++) {

if ([Link] != [Link]) {

return false; // Not a palindrome

firstHalf = [Link];

secondHalf = [Link];

return true; // It's a palindrome

class ListNode {

int val;

ListNode next;

ListNode(int val) {
[Link] = val;

[Link] = null;

You might also like