## DEV Community

Chidozie C. Okafor

Posted on • Originally published at doziestar.Medium on

# LinkedList To a 6 years old

A linked list is a data structure that consists of a chain of connected elements called “nodes.” Each node stores some data and a pointer to the next node in the chain.

Think of a linked list like a group of friends holding hands in a line. Each person represents a node in the linked list, and the hand they are holding represents the pointer to the next person in the line.

For example, let’s say we have a group of friends: Alice, Bob, Charlie, and Dave. Alice is holding Bob’s hand, Bob is holding Charlie’s hand, and Charlie is holding Dave’s hand. This forms a linked list of four nodes.

If we want to add a new friend, Emily, to the linked list, we can simply have Alice let go of Bob’s hand and hold Emily’s hand instead. Now the linked list looks like this: Alice, Emily, Bob, Charlie, and Dave.

We can also remove a friend from the linked list by having their neighbors let go of their hands. For example, if we want to remove Charlie from the linked list, we can have Bob let go of Charlie’s hand and hold Dave’s hand instead. Now the linked list looks like this: Alice, Emily, Bob, and Dave.

Linked lists are useful for storing and manipulating large amounts of data because they are flexible and efficient. They can grow and shrink as needed, and it is easy to add or remove elements from the list.

So the next time you are holding hands with your friends in a line, think about how you are forming a linked list!

Here’s a Python code example that demonstrates how to create and manipulate a linked list:

``````class Node:
def __init__ (self, data=None):
self.data = data
self.next = None

def __init__ (self):

new_node = Node(data)

def remove_node(self, data):
previous = None
found = False

if current.data == data:
found = True
else:
previous = current
current = current.next

if previous is None:
else:
previous.next = current.next

# Create a new linked list

while current is not None:
print(current.data)
current = current.next

# Remove a friend from the linked list
friends.remove_node("Charlie")

# Print the linked list again
while current is not None:
print(current.data)
current = current.next
``````

In this example, we define a Node class to represent a single node in the linked list, and a LinkedList class to represent the linked list itself. The LinkedList class has an add_node method to add a new node to the linked list, and a remove_node method to remove a node from the linked list.

We create a new linked list and add some friends to it using the add_node method. Then we iterate through the linked list and print the data of each node. Finally, we remove a friend from the linked list using the remove_node method, and iterate through the linked list again to print the updated list.

This code demonstrates how to create and manipulate a linked list in Python, using the concepts described in the above explanation.

Let’s solve some sorting problems

Suppose we have a list of integers that we want to sort in ascending order. One way to solve this problem is to use a linked list.

Here’s an example of how to solve this problem using a linked list in Python:

``````class Node:
def __init__ (self, data=None):
self.data = data
self.next = None

def __init__ (self):

new_node = Node(data)

def remove_node(self, data):
previous = None
found = False

if current.data == data:
found = True
else:
previous = current
current = current.next

if previous is None:
else:
previous.next = current.next

def sort(self):

while current is not None:
next_node = current.next
current = next_node

# Create a new linked list

numbers.sort()

# Print the sorted linked list