Skip to content

Instantly share code, notes, and snippets.

@sricharankrishnan
Created April 5, 2023 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sricharankrishnan/1c81444efff28225ad02b19a1a521246 to your computer and use it in GitHub Desktop.
Save sricharankrishnan/1c81444efff28225ad02b19a1a521246 to your computer and use it in GitHub Desktop.
A circular linked list data structure implementation done in java along with respective time complexities for each method
/**
* Circular Linked List
* public void deleteAtIndex(int index) - O(n)
* public boolean search(E value) - O(n)
* public Node get(int index) - O(n)
* public void insertAtHead(E value) - O(1)
* public void insertAtTail(E value) - O(1)
* public void insertAtIndex(E value, int index) - O(n)
*/
import java.util.*;
public class CircularLinkedList<E> {
private class Node<E> {
public E value;
public Node next;
public Node(E value) {
this.value = value;
this.next = null;
}
public String toString() {
return "[Node: " + this.value + "]";
}
}
private Node head;
private Node tail;
private int size;
public CircularLinkedList() {
this.head = this.tail = null;
this.size = 0;
}
/* O(n) */
public void deleteAtIndex(int index) {
if (index >= 0 && index < this.size()) {
if (this.size() == 1) {
this.head = this.tail = null;
}
else if (index == 0) {
this.head = this.head.next;
this.tail.next = this.head;
}
else {
Node prev = null;
Node next = null;
for (int i = 0; i < index; ++i) {
prev = (i == 0) ? this.head : prev.next;
}
next = prev.next.next;
prev.next = next;
}
--this.size;
}
}
/* O(n) */
public boolean search(E value) {
boolean wasFound = false;
if (!Objects.isNull(value) && !this.isEmpty()) {
Node node = null;
for (int i = 0; i < this.size; ++i) {
node = (i == 0) ? this.head : node.next;
if (node.value.equals(value)) {
wasFound = true;
break;
}
}
}
return wasFound;
}
/* O(n) */
public Node get(int index) {
Node node = null;
if (!this.isEmpty() && (index >= 0 && index < this.size())) {
for (int i = 0; i <= index; ++i) {
node = (i == 0) ? this.head : node.next;
}
}
return node;
}
/* O(1) */
public void insertAtHead(E value) {
if (!Objects.isNull(value)) {
Node node = new Node(value);
if (this.isEmpty()) {
this.head = this.tail = node;
}
else {
node.next = this.head;
this.tail.next = node;
this.head = node;
}
++this.size;
}
}
/* O(1) */
public void insertAtTail(E value) {
if (!Objects.isNull(value)) {
Node node = new Node(value);
if (this.isEmpty()) {
this.head = this.tail = node;
}
else {
node.next = this.head;
this.tail.next = node;
this.tail = node;
}
++this.size;
}
}
/* O(n) */
public void insertAtIndex(E value, int index) {
if (!Objects.isNull(value) && (index >= 0 && index <= this.size)) {
if (index == 0) {
this.insertAtHead(value);
}
else if (index == this.size) {
this.insertAtTail(value);
}
else {
Node node = new Node(value);
Node prev = null;
Node next = null;
for (int i = 0; i < index; ++i) {
prev = (i == 0) ? this.head : prev.next;
}
next = prev.next;
prev.next = node;
node.next = next;
++this.size;
}
}
}
public boolean isEmpty() {
return this.size() == 0;
}
public int size() {
return this.size;
}
public String toString() {
String string = "";
if (!this.isEmpty()) {
Node node = null;
for (int i = 0; i < this.size; ++i) {
node = (i == 0) ? this.head : node.next;
string += (i == this.size - 1) ? node : node + " <--> ";
}
}
return string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment