Given a singly linked list, sort it using bubble sort by swapping nodes.
Input: 10->30->20->5 Output: 5->10->20->30 Input: 20->4->3 Output: 3->4->20
- Get the Linked List to be sorted
- Apply Bubble Sort to this linked list, in which, while comparing the two adjacent nodes, actual nodes are swapped instead of just swapping the data.
- Print the sorted list
Below is the implementation of the above approach:
Linked list before sorting 5 -> 1 -> 32 -> 10 -> 20 -> 78 -> Linked list after sorting 1 -> 5 -> 10 -> 20 -> 32 -> 78 ->
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- C Program for Bubble Sort on Linked List
- Bubble Sort On Doubly Linked List
- Swap nodes in a linked list without swapping data
- Recursive selection sort for singly linked list | Swapping node links
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Linked List Sum of Nodes Between 0s
- Bubble Sort
- Sort a linked list of 0s, 1s and 2s
- Sum of all distinct nodes in a linked list
- Linked List Product of Nodes Between 0s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SHUBHAMSINGH10