Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Sorting a Linked List Data Structure | DSA Tutorial

YASH PAL, 12 May 20204 May 2026

Sorting linked list: There are many algorithms to sort a linked list, like the bubble sort, merge sort, etc., so today we are going to sort a linked list by exchanging the values of nodes using the bubble sort algorithm.

Sorting a linked list using the bubble sort algorithm

In a bubble sort algorithm to sort a linked list that has n items, we need to iterate over the list n-1 times. Like in the example, we have a list of 5 items, then we need to iterate over the list for 4 times.

To sort a linked list, first, we need three references: p, q, and end. In the first iteration, we set the end variable to None. And in every iteration, we set the p variable to the first node of the list and the q variable to the second node of the list.

Sorting a linked list using the bubble sort algorithm
Figure 1: Sorting a Linked List

If in the iteration the value of the linked part of node p is equal to the end, then we break the iteration and continue to the next iteration.

If the info part of node p is greater than the info part of node q, then we swap the values of node p and q; otherwise, we refer p and q to the next node of the list. So in the given example list, the value of node p is not greater than the value of the q node, so we refer to p and q as the next node of the list.

sorting a Linked list Data structures

Here, the value of node p is not greater than the value of node q, so we go to the next nodes.

sorting a Linked list Data structures

Here, the value of node p is greater than the value of node q, so we swap them.

sorting a Linked list Data structures

Next, we go to the next nodes. Here, the value of node p is not greater than the value of node q, so we don’t swap them.

sorting a Linked list Data structures

Now we go to the next nodes of the list. Here, the value of the linked part of node p is equal to the end variable. So here the first iteration ends.

sorting a Linked list Data structures

Now, after iteration one ends, the last node of the list becomes sorted, so we refer to the end variable as the last node of the list.

sorting a Linked list Data structures

Now the second iteration starts, and in the second iteration, we refer to the variables p and q as the first and second node of the list and swap the values of nodes p and q if the value of node p is greater than the value of node q.

This condition will run till the linked part of node p does not become equal to the end variable, as you see in the figures below.

sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list

And after every iteration, we update the value of the variable end. because if we iterate the list over two times, then the last two nodes of the list become sorted. So we don’t need to check them again.

sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list
sorting a linked list

So after performing the n-1 iterations over the linked list. where n is the size of the list. Now our list becomes sorted.

def bubble_sort_exdata(self):
    end = None
    while end != self.start.link:
        p = self.start
        while p.link != end:
            q = p.link
            if p.info > q.info:
                p.info, q.info = q.info, p.info
            p = p.link
        end = p

Data Structures & Algorithms Tutorials for Beginners
Computer Science Tutorials Data Structures Tutorials computer scienceData StructureDSA Tutorials

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes