Implement a circular linked list. A circular linked list is a variation of a linked list in which the last element is linked back to the first element, forming a circle. The list should support the following operations:
insert(int val): Insert an element into the circular linked list.
delete(int val): Delete an element from the circular linked list.
search(int val): Search for an element in the circular linked list.
display(): Display all the elements in the circular linked list
Input:
["insert(1)","insert(2)","insert(3)","display()","search(2)","delete(2)","display()"]Output:
[null,null,null,[1,2,3],true,null,[1,3]]Explanation:
1. Insert 1: The list becomes [1].2. Insert 2: The list becomes [1,2].3. Insert 3: The list becomes [1,2,3].4. Display: Displays the list [1,2,3].5. Search 2: Returns true as 2is found in the list.6. Delete 2: The list becomes [1,3].7. Display: Displays the list [1,3].
A circular linked list connects the last node back to the first node. Maintaining both a head and a tail pointer allows efficient insertion at both the head and the tail. The tail pointer helps to avoid traversing the entire list when inserting a new element at the end.
classListNode {
int val;
ListNode next;
ListNode(int val) {
this.val= val;
this.next=null;
}
}
classCircularLinkedList {
private ListNode head;
private ListNode tail;
publicCircularLinkedList() {
head =null;
tail =null;
}
// Insert at the head of the circular linked listpublicvoidinsertAtHead(int val) {
ListNode newNode =new ListNode(val);
if (head ==null) {
head = newNode;
tail = newNode;
newNode.next= head;
} else {
newNode.next= head;
head = newNode;
tail.next= head;
}
}
// Insert at the end of the circular linked listpublicvoidinsertAtEnd(int val) {
ListNode newNode =new ListNode(val);
if (head ==null) {
head = newNode;
tail = newNode;
newNode.next= head;
} else {
tail.next= newNode;
tail = newNode;
tail.next= head;
}
}
// Delete an element from the circular linked listpublicvoiddelete(int val) {
if (head ==null) {
return;
}
ListNode current = head;
ListNode prev =null;
while (current.val!= val) {
if (current.next== head) {
return; // Value not found }
prev = current;
current = current.next;
}
if (current == head) {
if (head == tail) {
head =null;
tail =null;
} else {
head = head.next;
tail.next= head;
}
} elseif (current == tail) {
tail = prev;
tail.next= head;
} else {
prev.next= current.next;
}
}
// Search for an element in the circular linked listpublicbooleansearch(int val) {
if (head ==null) {
returnfalse;
}
ListNode current = head;
do {
if (current.val== val) {
returntrue;
}
current = current.next;
} while (current != head);
returnfalse;
}
// Get the element at the specified indexpublicintget(int index) {
if (head ==null) {
thrownew IndexOutOfBoundsException();
}
ListNode current = head;
int count = 0;
do {
if (count == index) {
return current.val;
}
count++;
current = current.next;
} while (current != head);
thrownew IndexOutOfBoundsException();
}
// Display all elements in the circular linked listpublic List<Integer>display() {
List<Integer> ans =new ArrayList<>();
if (head ==null) {
return ans;
}
ListNode current = head;
do {
ans.add(current.val);
current = current.next;
} while (current != head);
return ans;
}
}