Doubly Linked List

Data Structure

Doubly Linked List

▾

HEAD

→

12

[0]

PTRS

•

HEAD

⇄

28

[1]

PTRS

•

⇄

64

[2]

PTRS

Ø

TAIL

⇄

NULL

Doubly Linked List ready. Select an operation tab below.

Size:

3 / 7

Time:

O(1)

Insert @ Idx:

Shortcuts: H Head • T Tail • F Find • D Delete • C Clear • M Sound • Z Focus

Implementation Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

class DoubleNode<T> {
  value: T;
  next: DoubleNode<T> | null = null;
  prev: DoubleNode<T> | null = null;
  constructor(value: T) {
    this.value = value;
  }
}

class DoublyLinkedList<T> {
  head: DoubleNode<T> | null = null;
  tail: DoubleNode<T> | null = null;
  size = 0;

  insertHead(value: T): void {
    const node = new DoubleNode(value);
    if (!this.head) {
      this.head = node;
      this.tail = node;
    } else {
      node.next = this.head;
      this.head.prev = node;
      this.head = node;
    }
    this.size++;
  }

  insertTail(value: T): void {
    const node = new DoubleNode(value);
    if (!this.tail) {
      this.head = node;
      this.tail = node;
    } else {
      this.tail.next = node;
      node.prev = this.tail;
      this.tail = node;
    }
    this.size++;
  }

  delete(value: T): boolean {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        if (current.prev) {
          current.prev.next = current.next;
        } else {
          this.head = current.next;
        }
        if (current.next) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev;
        }
        this.size--;
        return true;
      }
      current = current.next;
    }
    return false;
  }
}

Complexity Analysis

Access

O(n)

Search

O(n)

Insertion

O(1)

Deletion

O(1)

Space Complexity

O(n)

Color Legend

Primary Node (Top / Front / Head)

Inspected / Peeked

Element / Node

A Doubly Linked List is a linear data structure composed of sequentially linked nodes. Unlike a singly linked list, each node contains two pointers: one referencing the previous node and another referencing the next node. This allows full bidirectional traversal and O(1) removals when a pointer to a node is already available.