Data Structures: Linked List I
Tags: beginners, programming, algorithms, computerscience
Published: 2022-04-15 14:33:36


This is a 2-part post on linked lists. In this first part we shall focus on explanation, visualization, and use cases, while in the [next part](https://dev.to/m13ha/data-structures-linked-list-ii-2icj) we shall look at implementation. So let's get into it.


### Linked List


![meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g44rsvg8u8fsqnelnvaq.jpg)

A linked list is a linear data structure whose nodes/elements are stored randomly in memory, unlike an array whose elements are stored in a contiguous (side-by-side) manner in memory. Each node in a linked list has a pointer to the next node in the list; This makes it possible to find the nodes even though they are not contiguous in memory.


This manner of randomly storing data in memory makes inserting and deleting data much faster than in other linear data structures like arrays.


Below is a representation of memory on a computer. Lets assume each block can store one node/element.


![memory block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vh2wh3g0ktvdp1fv7sf.png)

Say we have an array with 7 elements, they would look like this in memory.

![Storing an array in memory gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qu7sfg6h2g3jich41z2.gif)

Remember that we mentioned above that arrays store their data contiguously in memory; this allows direct accessing of elements  using their index in the array. A linked list on the other hand, can only access their data sequentially, meaning that to get to the 4th, 10th, or 100th node we must first start from the 1st node and work our way forward. Because each node is stored randomly in memory, only through the nodes preceding the node we're looking for, can we find it.

A Linked List with 7 nodes could look like this in memory.

![Storing a Linked List in memory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edw7tiqex3i8swf66p8e.gif)

This manner of storing data randomly in memory has its benefits, as we mentioned earlier, but in order to better understand why, let's look at how arrays add new elements to memory. 

Remember the array from before, say we wanted to increase the number of elements from 7 to 9. The new elements can't just be added to the end of the array because we don't know if those spaces in memory are occupied or not.


![Wrong way to update array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c082q24dkc7q9oiubwrh.gif)


So what happens instead is that the elements of the array are copied to a place in memory that can hold 9 nodes, before the new nodes are added in and the old array is deleted.

![adding elements to an array process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2yznhpdw4oxjm65c304.gif)

This cycle is the same regardless of the number of elements in the array, could be 10, 100, or a 1000 doesn't matter; This cycle will always repeat when new elements are added to the array. As you can already imagine this situation can get pretty expensive timewise.

Adding new nodes to a linked list is much faster because they can be placed in any free spaces in memory and don't need to be side by side. so adding 2 new nodes to our previous linked list would go like this.


![adding new elements to a linked list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13zudytr957ksyv9c2gv.gif)

As we can see, the process is a lot faster compared to arrays.

Now that we understand why linked lists are useful, let's take a proper look at the structure of a linked list.

### Structure Of Linked List

A linked list is a collection of nodes that contain data and a pointer to the next node on the list. From this definition we can infer that each node in a linked list is divided into 2 parts the data and a pointer to the next node.


![Node Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzmylks5pwtogan9pj9e.png)

A linked list keeps track of the first node in the list, which is known as the head. The node at the end of a linked list points to `null`.


![Linked List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy0t0yzq8zoe47jz33jx.png)

###Types Of Linked List

The above image is a kind of linked list known as a Singly-Linked List. This is a kind of linked list whose pointers all go in one direction. This linked list can only be traversed forwards, not backwards.

There are 2 other kinds of linked list the Doubly-Linked List and the Circular-Linked List.

#### Doubly-Linked List

![Doubly-linked list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaakvr0ex62o3gwmyjaf.png)

A doubly-linked list is a list that can be traversed both forwards and backwards. 


![double pointer node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pipze9mzjj9urrmggtiz.png)


Each node in a doubly-linked list has an extra pointer to the node before it, except the head and last node which point to `null`.


#### Circular Linked List


![Circular linked list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbevajc4hdab6dj1njxd.png)


A circular linked list is a doubly-linked list with no nodes pointing to `null`. Instead, the prev pointer of the head node points to the last node on the list, while the next pointer of the last node points to the head of the list.

### Use Cases

A linked list can be used in a lot of ways; some of which include:

- Building [Stacks and Queue](https://dev.to/m13ha/data-structures-stacks-and-queues-i-ild) data structures.

- Efficiently make use of memory in your application.

- Build audio/video player and applications that use next and previous buttons.

- Undo/Redo functions in word editors and other development applications.

- Web browser history tracker.


In my next post we'll look at how to create a linked list in Javascript and some of the common methods of a linked list. Thanks for reading.

[Part 2](https://dev.to/m13ha/data-structures-linked-list-ii-2icj)

![seal you soon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wynqom84sweqos03iryr.gif)