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 ->
- 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
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- 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 the nodes of a Circular Linked List
- Sum of the alternate nodes of linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.