Overview

Linked lists are linear data structures that store elements in nodes, which are not stored in consecutive memory locations, unlike arrays. Instead, each node contains its data and a reference (or link) to the next node in the sequence, allowing for dynamic memory allocation and flexibility in inserting or deleting nodes. This structure makes linked lists particularly suitable for applications where memory space is a premium and modifications to the data structure are frequent. However, unlike arrays, linked lists do not allow direct access to elements, which can lead to slower search times as it requires sequential traversal from the start of the list.

Visualization

There are a few kinds of linked lists (e.g. single-linked list, double linked list, circular linked list). However, single-linked lists seem to be the most prevalent in interviews, which is what we’ll focus on for now.

image.png

As you can see, a linked list is made up of nodes, which usually have 2 parameters: a value, and pointer to the next node in the sequence.

Linked Lists vs. Arrays

At first, linked lists and arrays may seem similar in terms of functionality, but there are some important differences between them, and such questions may be asked in technical interviews.

Now that we have a general idea on how linked lists work, let’s walk through some problems.

Example 1: Reverse Linked List

This is a classic linked list problem. You’re given a linked list, and you need to referse it, as shown in the example below.

image.png

Let’s start with brainstorming.