Singly Linked List

Data Structure

Singly Linked List

▾

HEAD

→

12

[0]

NEXT

•

HEAD

→

28

[1]

NEXT

•

→

64

[2]

NEXT

Ø

TAIL

→

NULL

Singly 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 • R Reverse • 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

63

64

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

class SinglyLinkedList<T> {
  head: ListNode<T> | null = null;
  size = 0;

  insertHead(value: T): void {
    const node = new ListNode(value);
    node.next = this.head;
    this.head = node;
    this.size++;
  }

  insertTail(value: T): void {
    const node = new ListNode(value);
    if (!this.head) {
      this.head = node;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = node;
    }
    this.size++;
  }

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

  reverse(): void {
    let prev: ListNode<T> | null = null;
    let current = this.head;
    while (current) {
      const next = current.next;
      current.next = prev;
      prev = current;
      current = next;
    }
    this.head = prev;
  }
}

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 Singly Linked List is a linear data structure where elements are not stored at contiguous memory locations. Instead, each node contains a value and a reference pointer to the subsequent node. It enables dynamic memory allocation and efficient O(1) insertions or deletions at the head without resizing an array.