Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

LinkedList Implementation Analysis in Java

Tech Nov 10 18

LinkedList Overview

LinkedList implements both List and Deque interfaces using a doubly-linked list structure, contrasting with ArrayList's array-based implementation. Key characteristics include:

  • Non-thread-safe implementation (requires external synchronization)
  • Implements Cloneable for shallow copying
  • Supports serialization through Serializable
  • Functions as list, queue, or stack
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, Serializable { }

Internal Structure

Core components:

transient int elementCount = 0;
transient Node<E> head;
transient Node<E> tail;
private static final long serialVersionUID = 876323262645176354L;

private static class Node<T> {
    T value;
    Node<T> next;
    Node<T> previous;
    
    Node(Node<T> prev, T val, Node<T> nxt) {
        value = val;
        next = nxt;
        previous = prev;
    }
}

Key Operations

Element Access

public T getFirst() {
    final Node<T> initial = head;
    if (initial == null) throw new NoSuchElementException();
    return initial.value;
}

public T get(int position) {
    validateIndex(position);
    return getNode(position).value;
}

private Node<T> getNode(int idx) {
    if (idx < (elementCount >> 1)) {
        Node<T> current = head;
        for (int i = 0; i < idx; i++)
            current = current.next;
        return current;
    } else {
        Node<T> current = tail;
        for (int i = elementCount - 1; i > idx; i--)
            current = current.previous;
        return current;
    }
}

Element Insertion

public void addFirst(T element) {
    prepend(element);
}

private void prepend(T elem) {
    final Node<T> initial = head;
    final Node<T> newNode = new Node<>(null, elem, initial);
    head = newNode;
    if (initial == null)
        tail = newNode;
    else
        initial.previous = newNode;
    elementCount++;
    modCount++;
}

Element Removal

public T removeFirst() {
    final Node<T> initial = head;
    if (initial == null) throw new NoSuchElementException();
    return unlinkFirst(initial);
}

private T unlinkFirst(Node<T> node) {
    final T content = node.value;
    final Node<T> successor = node.next;
    node.value = null;
    node.next = null;
    head = successor;
    if (successor == null)
        tail = null;
    else
        successor.previous = null;
    elementCount--;
    modCount++;
    return content;
}

Queue Operations

public T peek() {
    final Node<T> initial = head;
    return (initial == null) ? null : initial.value;
}

public T poll() {
    final Node<T> initial = head;
    return (initial == null) ? null : unlinkFirst(initial);
}

public boolean offer(T elem) {
    return add(elem);
}

Iteration Mechanisms

Standard List Iterator

private class ListIteratorImpl implements ListIterator<T> {
    private Node<T> lastAccessed;
    private Node<T> upcoming;
    private int nextPosition;
    private int expectedModCount = modCount;
    
    public boolean hasNext() {
        return nextPosition < elementCount;
    }
    
    public T next() {
        checkModification();
        if (!hasNext()) throw new NoSuchElementException();
        lastAccessed = upcoming;
        upcoming = upcoming.next;
        nextPosition++;
        return lastAccessed.value;
    }
    // Additional methods omitted for brevity
}

Serialization Handling

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeInt(elementCount);
    for (Node<T> x = head; x != null; x = x.next)
        out.writeObject(x.value);
}

private void readObject(ObjectInputStream in) 
    throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    int count = in.readInt();
    for (int i = 0; i < count; i++)
        append((T) in.readObject());
}

Key Characteristics

  • Doubly-linked list enables bidirectional traversal
  • O(1) time complexity for insertions/deletions at ends
  • O(n) time for positional access
  • No inherent capacity constraints
  • Not thread-safe by default

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.