DEV Community

Chidozie C. Okafor
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

class LinkedList:
    def __init__ (self):
        self.head = None

    def add_node(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

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

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

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

# Create a new linked list
friends = LinkedList()

# Add some friends to the linked list
friends.add_node("Alice")
friends.add_node("Bob")
friends.add_node("Charlie")
friends.add_node("Dave")

# Print the linked list
current = friends.head
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
current = friends.head
while current is not None:
    print(current.data)
    current = current.next
Enter fullscreen mode Exit fullscreen mode

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

class LinkedList:
    def __init__ (self):
        self.head = None

    def add_node(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

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

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

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

    def sort(self):
        current = self.head
        sorted_list = LinkedList()

        while current is not None:
            next_node = current.next
            sorted_list.add_node(current.data)
            current = next_node

        self.head = sorted_list.head

# Create a new linked list
numbers = LinkedList()

# Add some numbers to the linked list in random order
numbers.add_node(5)
numbers.add_node(3)
numbers.add_node(1)
numbers.add_node(4)
numbers.add_node(2)

# Sort the linked list
numbers.sort()

# Print the sorted linked list
current = numbers.head
while current is not None:
    print(current.data)
    current = current.next
Enter fullscreen mode Exit fullscreen mode

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. We also define an add_node method to add a new node to the linked list, a remove_node method to remove a node from the linked list, and a sort method to sort the linked list.

We create a new linked list and add some numbers to it in random order. Then we call the sort method to sort the linked list, and iterate through the linked list to print the sorted list.

This code demonstrates how to use a linked list to solve the problem of sorting a list of integers in ascending order. The sort method works by iterating through the linked list and adding each node to a new, sorted linked list. This new linked list is then used to replace the original linked list, resulting in a sorted list.

Top comments (0)