Linked list coding challenge
Tags: algorithms, javascript, codenewbie, tutorial
Published: 2021-09-27 16:00:12

Today, we’ll be solving a [freecodecamp](https://www.freecodecamp.org/) algorithm question: https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index, removing an element from a linked list by index. Try to contain your excitement! 😂


![excited.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1631550964910/YNAJOC4Pr.gif)

---

We recommend reading [Arrays and Lists 📚](https://bytesized.news/issue-1-arrays-and-lists) before starting the coding exercise. This will ensure you have a firm understanding of the data structures used in the following exercise.

**⚡ TL;DR:** In linked list, elements are removed by linking the previous element with the next element, thus removing any link to the element to be removed.

### Setup

Before we get started, let’s picture yourself waiting in line at the grocery store. The line you’re waiting in will be our linked list.

Each person can only see the person in front of them, but they can’t see who is behind them. Bummer! 😞

Now that we have an idea of what a linked list looks like, let’s set it up. We’ll do this in JavaScript.

![carbon_2830_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551013035/Vhz-jXTJf.png)

The `LinkedList` has a `head`, this is the first `Node` of the list. Think of it as the last person standing in the line, they can see someone in front of them, but no one is standing behind them.

![line.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551070638/LOJP_qnP6.gif)

Each `Node` contains a value and a link to the next `Node` in the list.

Now, let’s create a list:

![carbon_2847_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551120942/pi34w6nIm.png)

Here, our `head`‘s value is `0`. The rest of the elements are called the `tail`.

![carbon_2841_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551164235/ZLVDBtbic.png)

### Walking the list 🚶‍♀️

In this step we’re going to traverse the list, node by node. As we can see, we’re starting with a list of five elements. For now, we just want to look at *every* element of the list.

![carbon_2832_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551328376/6YQvmt_xl.png)


It’s important to remember that the last node in the list will be pointing to `null` (this is the person at the front of the line). Once we reach this node, `node = node.next` will set `node` to `null` and we will stop the iteration.

This is like asking to the last person waiting in line who’s in front of them, then asking the same thing to that person, until you reach the person at the front of the line.

![carbon_2842_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551395149/DgukNjaj-.png)

### Finding a node

![books.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551451422/ymPI9uU-u.gif)

Now that we know how to traverse the entire list, we want to find the one we’re looking for. For that we’re going to need a counter, to keep track of the number of nodes we’ve seen so far when walking the list.

![carbon_2833_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551474000/K8H3B3TyL.png)

With the `counter` going up with every node, we can now compare it to the given `index`. When our `counter` is equal to `index`, we know we’ve reached the node we want to remove:

![carbon_2834_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551533152/xHbDLpREN.png)

### Removing the node

![wheres-my-book-cannot-find-it-still.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551569636/du16f1cNI.gif)

Having done the heavy lifting in the previous steps, removing the node has become a lot easier!

![carbon_2837_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551588114/LjVpw0EP9.png)

Let’s go through the steps, one more time:

1. We initialize a few variables:
    - `node`, that we set to the `head` (first element) of our linked list,
    - `counter`, which we’ll use to track the index of the node we’re looking at in the `while` loop,
    - `prev`, which we’ll set to the previous element we looked at in the `while` loop.
2. We start our loop, with a condition that says “don’t stop unless `node` is `null`”
3. We compare our `counter` to the `index` we want to remove.
    - If they’re equal, it’s time to remove the `node`! We make the previous node `prev` point to the next node in the list, `node.next` – now, no node in the list points to the one we want to remove anymore!
    - If not, we just keep going, updating `prev` to be the current `node`, and `node` to be the next one. We also increment our `counter`.

Think of it this way: with our group waiting in line, if someone in the middle of the line leaves, then the person behind them can now see the next person in front of them.

![carbon_2845_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551753747/FwA-k4wY_.png)

### Handling the edge cases

Now you might be wondering, what happens if the index is 0? Less than 0? What if it’s bigger than the length of our list? And you’re right, these are edge cases we will have to handle! Let’s see how we can do that:

![carbon_2836_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551792316/FCuCpGx0s.png)

As a bonus, here’s [this question from leetcode](https://leetcode.com/problems/reverse-linked-list/). We’ll solve it, but with a twist. 🤔 Can you spot the error?

![carbon_2846_29.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631551843608/0LimmJyEa.png)

*Hint:* do you have a way of finding where the list starts?

#### Before you go…

Thanks for reading! If you enjoyed this article, please give it a like 👍 to help others find it. And do not hesitate to share your thoughts in the comments below.

## 💡 Tip of the week

Are you learning git commands? Here is a resource you can use so you don’t have to remember it all: https://ohshitgit.com/

## 🔗 What else is going on in tech?

- Are you ready for [Facebook’s Metaverse](https://www.theverge.com/22588022/mark-zuckerberg-facebook-ceo-metaverse-interview)?
- The first release candidate for [Python 3.10](https://docs.python.org/3.10/whatsnew/3.10.html) is out!
- Looking for a tech podcast recommendation? Check out the [Elixir Mix](https://devchat.tv/podcasts/elixir-mix/)!