# What Is Linked List in Data Structure With Example?

//

Heather Bennett

A linked list is a fundamental data structure used in computer science to store a collection of elements. Unlike an array, which is a contiguous block of memory, a linked list consists of nodes that are connected through pointers.

What is a Node?
A node is the basic building block of a linked list. It contains two components: the data and the pointer to the next node. The data can be any type, such as an integer, string, or even another complex data structure.

Example:
Let’s consider an example of a linked list representing a sequence of numbers: 5 -> 10 -> 15 -> 20. Each number will be stored in a node.

## Creating a Node

In HTML, we can define the structure of a node using the <div> tag and assign classes to represent its components.

<div class="node">
<span class="data">5</span>
<span class="next"></span>
</div>

The .node class represents the entire node, while the .data class represents the data component. The .next class represents the pointer to the next node.

To create a linked list, we need to connect multiple nodes together. We do this by assigning the pointer of one node to another.

<div class="node first">
<span class="data">5</span>
<span class="next" id="pointer1"></span>
</div>

<div class="node">
<span class="data">10</span>
<span class="next" id="pointer2"></span>
</div>

<div class="node last">
<span class="data">15</span>
<span class="next"></span>
</div>

In this example, the first node has a pointer (#pointer1) to the second node, and the second node has a pointer (#pointer2) to the third node. The last node doesn’t have a next pointer since it is the end of the linked list.

To access all the elements in a linked list, we need to traverse through each node. We start from the first node and follow the next pointers until we reach the end of the list.

• Step 1: Set a temporary variable current to point to the first node.
• Step 2: Repeat until current is null (end of the list):
• a) Access the data of current.
• b) Move current to its next node using its next pointer.

Using this algorithm, we can iterate through each element in a linked list and perform various operations like searching for an element or modifying its data.

– Linked lists are dynamic data structures that can grow or shrink as needed.
– Insertion and deletion operations can be performed efficiently in a linked list.
– Linked lists don’t require contiguous memory allocation, unlike arrays.