Custom LinkedList

About

Implement a custom doubly linked list and add various operation

1. Data Structure

A LinkedList consists of nodes where each node stores:

  • A data value

  • A reference to the next node (next)

  • A reference to the previous node (prev) in the case of a doubly linked list.

Here’s how we define the Node class:

class Node<E> {
    E data;
    Node<E> next;
    Node<E> prev;

    Node(E data) {
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}

2. LinkedList Class

Our CustomLinkedList will maintain:

  • A head (first node)

  • A tail (last node)

  • The size of the list

3. Core Operations

Below are the essential operations for a doubly linked list.

3.1 Add Element at End (addLast)

3.2 Add Element at Beginning (addFirst)

3.3 Remove Element from End (removeLast)

3.4 Remove Element from Beginning (removeFirst)

3.5 Get Element at Index (get)

3.6 Print LinkedList (printList)

4. Complete Custom LinkedList Implementation

5. Time Complexity Analysis

Operation
Complexity

Add at the end (addLast)

O(1)

Add at the beginning (addFirst)

O(1)

Remove from the end (removeLast)

O(1)

Remove from the beginning (removeFirst)

O(1)

Get element at index (get)

O(n)

Print list (printList)

O(n)

Last updated