Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Sorting
  • MCQs on Sorting
  • Tutorial on Sorting
  • Bubble Sort
  • Quick Sort
  • Merge Sort
  • Insertion Sort
  • Selection Sort
  • Heap Sort
  • Sorting Complexities
  • Radix Sort
  • ShellSort
  • Counting Sort
  • Bucket Sort
  • TimSort
  • Bitonic Sort
  • Uses of Sorting Algorithm
Open In App
Next Article:
Alternate sorting of Linked list
Next article icon

Sorting a Singly Linked List

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a singly Linked List, the task is to sort this Linked List in non-decreasing order.

Examples:

Input: 10->30->20->5
Output: 5->10->20->30
Explanation: The above linked list is sorted in non-decreasing order.

Input: 20->4->3
Output: 3->4->20
Explanation: The above linked list is sorted in non-decreasing order.

Table of Content

  • Sort Linked List using Bubble Sort
  • Sort Linked List using Insertion Sort 
  • Sort Linked List using Quick Sort
  • Sort Linked List using Merge Sort
  • Which Sorting Algorithm is best for Linked Lists?

Sort Linked List using Bubble Sort

To apply Bubble Sort to a linked list, we need to traverse the list multiple times, comparing adjacent nodes and swapping their positions by adjusting their links if the current node’s data is greater than the next. During each pass, the largest unsorted element moves to its correct position at the end of the list. This process continues until no more swaps are needed, indicating that the list is sorted. Please refer to Bubble Sort for Linked List for implementation.

Time complexity: O(n^2) , where n is the number of nodes in the Linked List.
Auxiliary space: O(1)

Sort Linked List using Insertion Sort 

The algorithm for sorting a linked list using Insertion Sort involves gradually building a sorted portion of the list within the same memory space. For each node, the algorithm determines its correct position within the sorted list. If the node is smaller than the current head of the sorted list, it becomes the new head. Otherwise, it is inserted into its appropriate position by traversing the sorted list. This process is repeated until all nodes are correctly positioned, resulting in the sorted list. Please refer to Insertion Sort for Singly Linked List for implementation.

Time Complexity: O(n2), In the worst case, we might have to traverse all nodes of the sorted list for inserting a node.
Auxiliary Space: O(1), no extra space is required.

Sort Linked List using Quick Sort

Quick Sort is a highly efficient divide-and-conquer algorithm used for sorting linked lists. The algorithm operates by selecting a pivot element from the list and partitioning the remaining elements into two sublists, one containing elements smaller than the pivot and the other with elements larger. This partitioning step ensures that elements are grouped around the pivot, with all elements smaller placed before it and all larger placed after the pivot. The algorithm then recursively applies the same process to the sublists, progressively sorting the entire list. Please refer to QuickSort on Singly Linked List for implementation.

Time Complexity: O(n * log n), It takes O(n2) time in the worst case and O(n log n) in the average or best case.
Auxiliary Space: O(n)

Sort Linked List using Merge Sort

Merge Sort is a divide-and-conquer algorithm that splits the array into two halves, recursively sorts each half, and then merges the sorted halves back together. It requires additional memory to store temporary subarrays during the merge process. The algorithm repeatedly splits and merges until the entire array is sorted. Please refer to Merge Sort for Linked Lists for implementation.

Which Sorting Algorithm is best for Linked Lists?

For linked lists, Merge Sort is often the best choice because:

  • Merge Sort guarantees a time complexity of O(nlogn) in the average, best, and worst cases.
  • Merge Sort is Stable
  • Merge Sort naturally fits well with Linked List because it traverses items in sequential manner (no random access)
  • When we compare linked list implementation with the array implementation we can notice that the linked list implementation does not require extra space to merge because linked list allows insertion and deletion in the middle in O(1) time.

Next Article
Alternate sorting of Linked list

R

RishabhPrabhu
Improve
Article Tags :
  • Linked List
  • Sorting
  • DSA
  • Merge Sort
  • Quick Sort
  • Insertion Sort
  • BubbleSort
  • Linked-List-Sorting
Practice Tags :
  • Linked List
  • Merge Sort
  • Sorting

Similar Reads

  • Singly Linked List Tutorial
    A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations. U
    8 min read
  • Insertion Sort for Singly Linked List
    Given a singly linked list, the task is to sort the list (in ascending order) using the insertion sort algorithm. Examples: Input: 5->4->1->3->2Output: 1->2->3->4->5Input: 4->3->2->1Output: 1->2->3->4 The prerequisite is Insertion Sort on Array. The idea is
    8 min read
  • Alternate sorting of Linked list
    Given a linked list containing n nodes. The problem is to rearrange the nodes of the list in such a way that the data in first node is first minimum, second node is first maximum, third node is second minimum, fourth node is second maximum and so on. Examples: Input : list: 4->1->3->5->2
    4 min read
  • QuickSort on Singly Linked List
    Given a linked list, apply the Quick sort algorithm to sort the linked list. To sort the Linked list change pointers rather than swapping data. Example: Input: 5->4->1->3->2Output: 1->2->3->4->5Input: 4->3->2->1Output: 1->2->3->4 Approach: The Quick Sort alg
    13 min read
  • Merge Sort for Linked Lists
    Given a singly linked list, The task is to sort the linked list in non-decreasing order using merge sort. Examples: Input: 40 -> 20 -> 60 -> 10 -> 50 -> 30 -> NULLOutput: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULL Input: 9 -> 5 -> 2 -> 8 -> NULLOutput: 2
    12 min read
  • Merge K Sorted Linked Lists using Min Heap
    Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order. Examples: Input: K = 3, N = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->
    9 min read
  • Merge K sorted linked lists
    Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order. Examples: Input: Output: Merged lists in a sorted order where every element is greater than the previous element. Input: Output: Merged lists in a sorted order where every element is greater
    15+ min read
  • Traversal of Singly Linked List
    Traversal of Singly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a singly linked list along with its implementation. Examples: Input: 1->2->3->4->5->nullOutput
    11 min read
  • Singly Linked List in Python
    A Singly Linked List is a type of data structure that is made up of nodes that are created using self-referential structures. Each node contains a data element and a reference (link) to the next node in the sequence. This allows for a dynamic and efficient management of data elements. Table of Conte
    10 min read
  • Permutation of a Linked List
    Given a singly linked list of distinct integers, the task is to generate all possible permutations of the linked list elements. Examples: Input: Linked List: 1 -> 2 -> 3Output: 1 -> 2 -> 31 -> 3 -> 22 -> 1 -> 32 -> 3 -> 13 -> 1 -> 23 -> 2 -> 1Input: Linked L
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences