Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Sorting a Linked list in Data structures

YASH PAL, 12 May 202028 May 2024

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 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 Data structures

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 p and q to the next node of the list.

sorting a Linked list Data structures

here also 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 the end variable to the last node of the list.

sorting a Linked list Data structures

now the second iteration starts and in the second iteration, we refer the variables p and q to the first and second node of the list and swap values of nodes p and q if the values of node p are 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 images given below.

sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures

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 Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures

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
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes