# Singly Linked List

> The Singly Linked List is a linear sequence data structure of nodes where each node points to the next, forming a unidirectional link.

## What is a singly linked list and when should I use one?

The linear data structure in which each node contains data and a reference to the next node. Ideal for dynamic memory management, though traversal is one-way only.

- Topic: Linked List
- Type: data structure

## Example

Wrong:

```js
let prev = null, node = head;
while (node) {
  node.next = prev;
  prev = node;
  node = node.next;
}
```

Right:

```js
let prev = null, node = head;
while (node) {
  const next = node.next;
  node.next = prev;
  prev = node;
  node = next;
}
```

Saving `next` first keeps a handle on the rest of the list; otherwise the loop stops after the first node.

## Common mistakes

- Losing the rest of the list by overwriting `next` too early.
- Forgetting to update `head` when deleting the first node.

## Key points

- When reversing or relinking, the next pointer is saved before it is overwritten.
- Appends at the end keep a tail pointer; otherwise each append walks the list in O(n).
- Traversal loops check `node !== null` before reading `node.next`.

## Interview questions

### What is the time complexity of a singly linked list?

Insert or delete at the head is O(1). Access by index and search are O(n).

### Singly vs doubly linked list: what is the difference?

A doubly linked list also stores a previous pointer, so it can walk backward and delete a known node in O(1), at the cost of more memory.

### Does JavaScript have a built-in linked list?

No, JavaScript has no linked list type. Arrays cover most needs; write a small node class when you need one.

## Related cards

- [Linked List](https://wonderium.dev/algorithms/linked-list.md)
- [Doubly Linked List](https://wonderium.dev/algorithms/doubly-linked-list.md)
- [Node](https://wonderium.dev/algorithms/node.md)
- [Stack](https://wonderium.dev/algorithms/stack.md)
- [Dynamic Array](https://wonderium.dev/algorithms/dynamic-array.md)

---

- Level: [Algorithms](https://wonderium.dev/algorithms.md)
- HTML version: https://wonderium.dev/algorithms/singly-linked-list
- Last updated: 2026-11-25
