Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsHiringStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
DSA for Frontend Engineers
  1. 1.Introduction to DSA for Frontend Engineers
  2. 2.JavaScript Fundamentals for DSA
  3. 3.Arrays and Strings: Core Techniques
  4. 4.Objects and Hash Maps in JavaScript
  5. 5.Linked Lists: Building Blocks
  6. 6.Stacks and Queues: LIFO & FIFO
  7. 7.Recursion and Iteration: Problem Solving Paradigms
  8. 8.Introduction to Trees: Binary Trees & BSTs
  9. 9.Tree Traversal Algorithms (BFS & DFS)
  10. 10.Sorting Algorithms I: Basics
  11. 11.Sorting Algorithms II: Divide & Conquer
  12. 12.Searching Algorithms: Linear & Binary Search
  13. 13.Graphs: Representation & Traversals
  14. 14.Dynamic Programming I: Memoization
  15. 15.Heaps and Priority Queues
  16. 16.Tries (Prefix Trees) for Autocomplete & Search
  17. 17.Advanced Graph Algorithms: Shortest Paths & MST
  18. 18.Dynamic Programming II: Tabulation & Frontend Patterns
  19. 19.Frontend System Design with DSA Lens
  20. 20.Interview Strategies & Advanced Problem Solving
DSA for Frontend Engineers
05

DSA · Lesson 5 of 20 · 3 min

Linked Lists: Building Blocks

This is how you explain the trade-offs of arrays versus linked lists and show you can handle pointer manipulation.

When to use a linked list over an array

In an interview, you'll be asked to justify your choice of data structure. Saying "a linked list is dynamic" isn't enough. The real difference is in the cost of insertion and deletion.

Arrays store elements in a contiguous block of memory. This gives you O(1) random access by index. But if you insert an element at the beginning, you have to shift every other element over one spot. That's an O(n) operation. Array.prototype.unshift is expensive.

Linked lists are different. Each element, or "node," is a separate object that holds a value and a pointer to the next node. They can be scattered anywhere in memory. This means accessing the nth element requires traversing the list from the beginning—an O(n) operation.

The payoff is O(1) insertion and deletion at the beginning or end of the list. To add a new head, you just create a new node and point its next to the old head. No shifting required. This makes linked lists suitable for structures like queues or stacks where you primarily interact with the ends.

The anatomy of a linked list

A linked list is just a collection of nodes. Each node has two properties: the data it holds (value) and a reference to the next node in the chain (next). The list itself keeps track of the first node (head) and sometimes the last (tail).

The last node's next pointer is null, which signals the end of the list. If you lose the reference to the head, you've lost the entire list.

Here is the fundamental Node class. You should be able to write this from memory.

class Node<T> {
  value: T;
  next: Node<T> | null;

  constructor(value: T) {
    this.value = value;
    this.next = null;
  }
}

Implementing insertion

You will be expected to implement a linked list from scratch. The most important methods involve pointer manipulation. Focus on append (add to end) and prepend (add to start).

The trap: Forgetting to handle the edge case where the list is empty. When adding the first node, it becomes both the head and the tail. Messing this up is a common red flag.

class SinglyLinkedList<T> {
  head: Node<T> | null;
  tail: Node<T> | null;
  size: number;

  constructor() {
    this.head = null;
    this.tail = null;
    this.size = 0;
  }

  // O(1)
  append(value: T): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail!.next = newNode;
      this.tail = newNode;
    }
    this.size++;
  }

  // O(1)
  prepend(value: T): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.next = this.head;
      this.head = newNode;
    }
    this.size++;
  }
}

Notice both operations are O(1) because we maintain a tail pointer. If we only had a head pointer, append would require traversing the entire list to find the end, making it an O(n) operation.

Articulating the trade-offs in an interview

When you're asked to choose between an array and a linked list, structure your answer around time complexity for the specific operations required.

"For this problem, we need to frequently add and remove items from the beginning of the collection. Using a JavaScript array, unshift and shift are O(n) operations because they require re-indexing the entire array. A linked list provides O(1) insertion and deletion at the head, which would be much more performant as the collection grows. The trade-off is that we lose O(1) index-based access, but the problem doesn't require that."

This answer shows you understand the underlying mechanics and can apply them to a concrete problem.

Also, be aware of common linked list-based algorithms. Many problems, like detecting a cycle or finding the middle node, are solved efficiently using the "fast and slow pointer" (or "runner") technique. Mentioning this shows you're familiar with the problem space beyond basic implementation. You can practice this with Reverse a Linked List.

Try it

Implement a delete(value: T) method for the SinglyLinkedList class. Whiteboard it first, paying close attention to three distinct cases: deleting the head, deleting the tail, and deleting a node in the middle. Remember to update head, tail, and size correctly.

Next: We'll see how linked lists and arrays are used to build Stacks and Queues: LIFO & FIFO.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice this pattern
Previous

Objects and Hash Maps in JavaScript

Next

Stacks and Queues: LIFO & FIFO