Data Structure
Time Complexility
Operation | Singly LL | Doubly LL |
---|---|---|
Search | O(n) | O(n) |
Insert (head) | O(1) | O(1) |
Insert (Tail) | O(1) | O(1) |
Remove (head) | O(1) | O(1) |
Remove (Tail) | O(n) | O(1) |
Remove (middle) | O(n) | O(n) |
Implementation: Doubly Linked List
public class DoublyLinkedList<T> implements Iterable<T> {
private int size = 0;
private Node<T> head = null;
private Node<T> tail = null;
// Node class as inner class
private static class Node<T> {
private T data;
private Node<T> prev, next;
public Node(T data, Node<T> prev, Node<T> next){
this.data = data;
this.prev = prev;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
// other API methods
}
References: