Linked Lists Interview Questions

Linked lists are a collection of nodes that are connected by links or pointers. In this section, we'll power through some exercises to get you familiar with their inner workings.

The goal of this section is to become comfortable using linked list terminology and concepts. We'll start by practicing the basic operations on a linked list — adding and removing elements — and then we'll move on to more advanced topics like finding specific elements in a list.

Section Menu

How do I use this section?

1. CHALLENGE

Find the Intersection of Two Linked Lists

Assume you have a Linked List implementation that has the following API: // prepend to start of list #prepend(val); // appends to end of list #append(val); // get an array of node values (for testing) #toArray(); Can you write a method getIntersection(list1, list2) to find the intersection of two linked lists? <img...

2. CHALLENGE

Grab a Random Node

Given a linked list, can you write a method to get a random node within it? Let's assume you're given a random node generator. The linked list will have at least 2 nodes, and may look something like this: 1 -> 2 -> 3 -> 4 The odds of getting any number between 1 and 4 inclusive should be the exactly the same. <img...

3. CHALLENGE

Swap Every Two Nodes in a Linked List

Write a recursive algorithm that swaps every two nodes in a linked list. This is often called a pairwise swap. For example: `js /* original list 1 -> 2 -> 3 -> 4 after...

4. CHALLENGE

Union of Linked Lists

Now that we've implemented a Linked List, let's start operating on it! Assume you have a Linked List implementation with this definition: `js class LinkedList { constructor() { this.head = null; this.tail = null; } prepend(newVal) { const currentHead = this.head; cons...

5. CHALLENGE

Delete Nodes From A Linked List

Now that we've implemented a linked list, can you write a method that will delete all nodes of a given value? You're given the foll...

6. CHALLENGE

Implement a Linked List

Let's implement a linked list, a data structure that serves as one of the fundamental building blocks of many other data structures. A linked list is defined as a linear data structu...

7. CHALLENGE

Merge Sorted Linked Lists

Write an algorithm to merge two sorted linked lists and return it as a new sorted list. The new list should be constructed by joining the nodes of the input lists. You may a...

8. CHALLENGE

Delete Node From End

Removing the Nth Node From a Linked List's End: A Single-Pass Approach Get ready to sail through a classic problem: Removing the nth node from the end of a linked list in a single pass. By the end of this lesson, you'll be able to wave goodbye to that pesky nth node and still maintain the list's structure. ![Linked List Vi...

9. CHALLENGE

Add Linked List Numbers

We're provided the following two linked lists: 1 -> 2 -> 3 -> 4 and 2 -> 5 -> 8 Each one represents a number in reversed order, so 1 -> 2 -> 3 -> 4 represents 4321 and 2 -> 5 -> 8 represents 852 (note: for an extra challenge, consider if they weren't reversed). The lists are guaranteed to have at least one node and...

10. CHALLENGE

Detect Loop in Linked List

What is the best way to find out if a linked list contains a loop? You're given the following data structure as the definition of a linked list node: `js class LinkedListNode { constructor(val) { this.val = val; this.next = null; } } ` A loop or a cycle in graph...

11. CHALLENGE

Merge K (Multiple) Sorted Lists

You're given an array of multiple sorted linked lists. Can you write a method that returns it transformed into a single sorted linked list? Each linked list can be represe...

12. CHALLENGE

Clone Random Pointer Linked List

You are given a head of a linked list of length n such that each node contains an additional random pointer. This random pointer may point to any other node in the list or is null. Create a deep copy of the given linked list, and return its head such that no additional space is used. The deep copy should consist...