Linked List Visualization
Explore linked list operations with real-time visualization and step-by-step Java code execution. Watch how nodes are inserted, deleted, and searched in a singly linked list.
Linked list is empty. Add nodes to get started!
Nodes:
0
Head:
null
Last Operation:
None
Operation Explanation
Select an operation from the controls panel to see the step-by-step execution with highlighted code and detailed explanations.
LinkedList.java
View Source
public class LinkedList {
private Node head;
private int size = 0;
private class Node {
T data;
Node next;
Node(T data) {
this.data = data;
this.next = null;
}
}
public void insertAtHead(T data) {
Node newNode = new Node<>(data);
newNode.next = head;
head = newNode;
size++;
}
public void insertAtTail(T data) {
Node newNode = new Node<>(data);
if (head == null) {
insertAtHead(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
size++;
}
public void insertAtIndex(int index, T data) {
if (index < 0 || index > size) {
return;
}
if (index == 0) {
insertAtHead(data);
return;
}
Node newNode = new Node<>(data);
Node current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
size++;
}
public boolean delete(T data) {
if (head == null) {
return false;
}
if (head.data.equals(data)) {
head = head.next;
size--;
return true;
}
Node current = head;
while (current.next != null) {
if (current.next.data.equals(data)) {
current.next = current.next.next;
size--;
return true;
}
current = current.next;
}
return false;
}
public int search(T data) {
Node current = head;
int index = 0;
while (current != null) {
if (current.data.equals(data)) {
return index;
}
current = current.next;
index++;
}
return -1;
}
public int size() {
return size;
}
}
Linked List Operations
Animation Controls
Slow
Fast
Presets
Statistics
Operations:
0
Traversals:
0
Time Complexity (Last Operation):
-