### Linked List Operations in Java
## Singly Linked List (SLL)
```java
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class SinglyLinkedList {
Node head;
public void insertAtHead(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void insertAtTail(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void insertAtPosition(int data, int position) {
if (position < 1) return;
if (position == 1) {
insertAtHead(data);
return;
}
Node newNode = new Node(data);
Node temp = head;
for (int i = 1; temp != null && i < position - 1; i++) {
temp = temp.next;
}
if (temp == null) return;
newNode.next = temp.next;
temp.next = newNode;
}
public void deleteFromHead() {
if (head == null) return;
head = head.next;
}
public void deleteFromTail() {
if (head == null || head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
public void deleteAtPosition(int position) {
if (head == null || position < 1) return;
if (position == 1) {
deleteFromHead();
return;
}
Node temp = head;
for (int i = 1; temp != null && i < position - 1; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) return;
temp.next = temp.next.next;
}
public void printForward() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
}
```
## Doubly Linked List (DLL)
```java
class NodeDLL {
int data;
NodeDLL next, prev;
public NodeDLL(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
NodeDLL head, tail;
public void insertAtHead(int data) {
NodeDLL newNode = new NodeDLL(data);
if (head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
}
public void insertAtTail(int data) {
NodeDLL newNode = new NodeDLL(data);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
public void deleteFromHead() {
if (head == null) return;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
head.prev = null;
}
}
public void deleteFromTail() {
if (tail == null) return;
if (head == tail) {
head = tail = null;
} else {
tail = tail.prev;
tail.next = null;
}
}
public void printForward() {
NodeDLL temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
public void printBackward() {
NodeDLL temp = tail;
while (temp != null) {
System.out.print(temp.data + " <- ");
temp = temp.prev;
}
System.out.println("NULL");
}
}
```
## Circular Linked List (CLL)
```java
class NodeCLL {
int data;
NodeCLL next;
public NodeCLL(int data) {
this.data = data;
this.next = null;
}
}
class CircularLinkedList {
NodeCLL head, tail;
public void insertAtEnd(int data) {
NodeCLL newNode = new NodeCLL(data);
if (head == null) {
head = tail = newNode;
tail.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
public void deleteFromHead() {
if (head == null) return;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
tail.next = head;
}
}
public void printList() {
if (head == null) return;
NodeCLL temp = head;
do {
System.out.print(temp.data + " -> ");
temp = temp.next;
} while (temp != head);
System.out.println("HEAD");
}
}
```
This contains insertion, deletion, forward and backward traversal for all linked list types.