Data Structures: Linked Lists

Data Structures: Linked Lists:

 So to begin with, let us define the linked list.

  • A linked list is a data structure.
  • It’s linear, meaning elements are in a sequence (ex. 1 2 3 4 5).
  • Elements of a linked list are NOT stored sequentially to each other in memory(RAM).
  • Elements are linked to each other’s using pointers.

Below is an image visualizing linked lists:

Let us break this image down:

  1. Head: The first element of a linked list (in this case it is the element at memory address 3200)
  2. Content: Your data (can be anything you’d want ex. string, ints, floats, etc…)
  3. Pointer: Address in memory to the next element (node).
  4. Tail: Last element of a linked list (you can identify a tail by it having always a pointer of next element to null)

Where the hell are they used?

As programmers, our first instinct is to see how we can use this in our current or new projects, and this is valid thinking.

We don’t wanna be studying something that we might never use.


Well, that is why I’ve compiled down a list of use-cases for linked lists.

  • Implementation of Stacks and Queues (FIFO)
  • Implementation of Graphs
  • Dynamic memory allocation
  • Maintaining directory of names
  • Performing arithmetic operations on long integers
  • Manipulation of Polynomials
  • Representing sparse matrices

Types of Linked Lists

There are three types of linked lists:

Single Linked List

A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node(tail).

Single Linked List

Doubly Linked List

A doubly linked list is a type of linked list that is bidirectional, that is, it can be traversed in two directions, from head to the last node(tail) and vice-versa.

Doubly Linked List

Circular Linked List

A circular linked list is a type of linked list, in which the head points to the tail, and the tail points to the head. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

Singly Circular Linked List

Doubly Circular Linked List


Keep in mind when measuring the complexity of a data structure, we always look at the worst-case scenario. So we can imagine that the size of this linked list is in the millions or even billions.


Both single and doubly linked list has to traverse the whole linked list to find the specific element that we are searching for. That is why the complexity for is O(n).


Search is similar to access time, so it is also O(n).


Linked lists are used best in insertion and deletion due to the elements not needing to be explicitly sequential in memory.

Insertion is pretty simple.

For example, if you want to add an element to the beginning of a linked list(head). You simply create the element in some memory location and you point it to the previous head. This is why the time complexity is O(1).

Insertion in the middle of the list


Similar to insertion, the time complexity is O(1), because to delete your not gonna have to go through the whole list, shifting elements. You simply manipulate the pointers of the previous and next element that you want to delete.

Deletion in Linked List

Implementation of Linked List

Usually, to represent a node in a linked list, we create a class with these properties:

class Node:
    def __init__(self, data=None): = data
        self.nextval = None
  • Data: value of the data (string, int, etc…).
  • Nextval: pointer or reference to the next element in the list.

The standard linked list interface requires us to implement these methods:

  1. list – Display all elements/nodes.
  2. push – insert at the end of the list.
  3. insertAtBeginning – insert at the beginning of the list
  4. insert – inserting in between two Data Nodes
  5. remove – remove an existing node using the key for that node

Our base class will look like this:

class SLinkedList: # Single Linked List
    def __init__(self):
        self.headval = None 

Let’s implement the methods one by one.

Display all elements

class SLinkedList:
    def __init__(self):
        self.headval = None

    def list(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval

Insert at the beginning of the list

class SLinkedList:
    def __init__(self):
        self.headval = None

    def insertAtBeginning(self,newdata):
        NewNode = Node(newdata)

    # Update the new nodes next val to existing node
        NewNode.nextval = self.headval
        self.headval = NewNode

Insert at the end of the list

class SLinkedList:
    def __init__(self):
        self.headval = None

   # Function to add newnode
    def push(self, newdata):
        NewNode = Node(newdata)
        if self.headval is None:
            self.headval = NewNode
        laste = self.headval
            laste = laste.nextval

Insert between two nodes

class SLinkedList:
    def __init__(self):
        self.headval = None

    def insertBetween(self,middle_node,newdata):
        if middle_node is None:
            print("The mentioned node is absent")

        NewNode = Node(newdata)
        NewNode.nextval = middle_node.nextval
        middle_node.nextval = NewNode

Remove a node using it’s key

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

    def remove(self, Removekey):

        HeadVal = self.head

        if (HeadVal is not None):
            if ( == Removekey):
                self.head =
                HeadVal = None

        while (HeadVal is not None):
            if == Removekey:
            prev = HeadVal
            HeadVal =

        if (HeadVal == None):
            return =

        HeadVal = None

Keep in mind, here we are implementing a single-linked list. Implementing a double-linked list is similar, apart from the extra prev variable which will store the pointer or reference to the previous element. It will look something like this.

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

How to reverse a god damn linked list?

Congratulations, you know about linked lists.

Now, coming back to our main question.

How do we reverse a linked list?

Think about it, and then proceed to read the answer below.

To help you imagine this:

Input: 1→ 2→ 3→ 4

Output: 4 → 3 → 2 → 1


There are multiple ways to reverse a linked list, which includes:

  • Iterative Method
  • Recursive Method
  • Stack Method

For the sake of simplicity, we are going to use the iterative method.


  1. Initialize three pointers prev as NULL, curr as head and next as NULL.
  2. Iterate through the linked list. In the loop, do the following:
// Before changing next of current, 
// store next node 
next = curr->next
// Now change next of current 
// This is where actual reversing happens 
curr->next = prev 
// Move prev and curr one step forward 
prev = curr 
curr = next


Python Implementation

# Python program to reverse a linked list
# Time Complexity: O(n)
# Space Complexity: O(1)

# Node class

class Node:

    # Constructor to initialize the node object
    def __init__(self, data): = data = None

class LinkedList:

    # Function to initialize head
    def __init__(self):
        self.head = None

    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next =
   = prev
            prev = current
            current = next
        self.head = prev

    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data) = self.head
        self.head = new_node

    # Utility function to print the linked LinkedList
    def printList(self):
        temp = self.head
            temp =

# Driver code
llist = LinkedList()

print "Given Linked List"
print "\nReversed Linked List"

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)


In summary today we learned:

  1. Linked Lists and their properties.
  2. Where it is used
  3. Implementation
  4. How to reverse one

I hope you learned something today, and if you got any questions/suggestions feel free to comment them down in the description.

from Tumblr

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s