Retype This Passage
Java Singly Linked List: Add, Remove, Get, and Size
Practice typing a practical generic singly linked list in Java with append, remove, lookup, and size operations using clean pointer logic.
90s testintermediateCode passage
Instructions
Type the code exactly as shown. Preserve indentation, generic angle brackets, braces, semicolons, method names, and spacing.
Timer01:30
WPM0
Accuracy100%
Progress0%
Mistakes0
java-linked-list-implementation
import java.util.Objects;↵
↵
public class SinglyLinkedList<T> {↵
private static class Node<T> {↵
private final T value;↵
private Node<T> next;↵
↵
private Node(T value) {↵
this.value = value;↵
}↵
}↵
↵
private Node<T> head;↵
private Node<T> tail;↵
private int size;↵
↵
public void add(T value) {↵
Node<T> newNode = new Node<>(value);↵
↵
if (head == null) {↵
head = newNode;↵
tail = newNode;↵
} else {↵
tail.next = newNode;↵
tail = newNode;↵
}↵
↵
size++;↵
}↵
↵
public boolean remove(T value) {↵
Node<T> current = head;↵
Node<T> previous = null;↵
↵
while (current != null) {↵
if (Objects.equals(current.value, value)) {↵
if (previous == null) {↵
head = current.next;↵
} else {↵
previous.next = current.next;↵
}↵
↵
if (current == tail) {↵
tail = previous;↵
}↵
↵
size--;↵
return true;↵
}↵
↵
previous = current;↵
current = current.next;↵
}↵
↵
return false;↵
}↵
↵
public T get(int index) {↵
if (index < 0 || index >= size) {↵
throw new IndexOutOfBoundsException("Index: " + index);↵
}↵
↵
Node<T> current = head;↵
for (int i = 0; i < index; i++) {↵
current = current.next;↵
}↵
↵
return current.value;↵
}↵
↵
public int size() {↵
return size;↵
}↵
}
import java.util.Objects;↵
↵
public class SinglyLinkedList<T> {↵
private static class Node<T> {↵
private final T value;↵
private Node<T> next;↵
↵
private Node(T value) {↵
this.value = value;↵
}↵
}↵
↵
private Node<T> head;↵
private Node<T> tail;↵
private int size;↵
↵
public void add(T value) {↵
Node<T> newNode = new Node<>(value);↵
↵
if (head == null) {↵
head = newNode;↵
tail = newNode;↵
} else {↵
tail.next = newNode;↵
tail = newNode;↵
}↵
↵
size++;↵
}↵
↵
public boolean remove(T value) {↵
Node<T> current = head;↵
Node<T> previous = null;↵
↵
while (current != null) {↵
if (Objects.equals(current.value, value)) {↵
if (previous == null) {↵
head = current.next;↵
} else {↵
previous.next = current.next;↵
}↵
↵
if (current == tail) {↵
tail = previous;↵
}↵
↵
size--;↵
return true;↵
}↵
↵
previous = current;↵
current = current.next;↵
}↵
↵
return false;↵
}↵
↵
public T get(int index) {↵
if (index < 0 || index >= size) {↵
throw new IndexOutOfBoundsException("Index: " + index);↵
}↵
↵
Node<T> current = head;↵
for (int i = 0; i < index; i++) {↵
current = current.next;↵
}↵
↵
return current.value;↵
}↵
↵
public int size() {↵
return size;↵
}↵
}
The passage stays in place while your typed text lines up directly on top of it. Tip: use `Tab` for `⇄` and `Enter` for `↵` while typing the passage.
Correct chars: 0Typed chars: 0Mistakes: 0Status: Ready