Java program to sort the elements of the doubly linked list17 Mar 2025 | 4 min read In this program, we will create a doubly linked list and sort nodes of the list in ascending order. Original List: ![]() Sorted List: ![]() To accomplish this, we maintain two pointers: current and index. Initially, current point to head node and index will point to node next to current. Traverse through the list till current points to null, by comparing current's data with index's data. If the current's data is greater than the index's data, then swap data between them. In above example, current will initially point to 7, and the index will point to 1. Since, 7 is greater than 1, swap the data. Continue this process till the entire list is sorted in ascending order. Algorithm
a. sortList() will sort nodes of the list in ascending order.
a. display() will show all the nodes present in the list.
Program:Output: Original list: 7 1 4 5 2 Sorted list: 1 2 4 5 7 Next TopicJava Programs |
Java Program to Count the Total Number of Punctuation Characters Exists in a String In the ious Java string programs, we have counted the number of words, white spaces, vowels, consonants, and characters. Sometimes it also becomes necessary to count the total number of punctuations in...
2 min read
An array or list inArr of the numbers (either negative or positive) is given. The task is to find the number of smaller elements that exist on the right side of the current element. The following examples are given for a better understanding. Example 1: Input int inArr[] =...
14 min read
Java Program to find Smallest Number in an Array We can find the smallest element or number in an array in java by sorting the array and returning the 1st element. Let's see the full example to find the smallest number in java array. public class SmallestInArrayExample{ public static...
2 min read
Program to find the frequency of each element in the array In this program, we have an array of elements to count the occurrence of its each element. One of the approaches to resolve this problem is to maintain one array to store the counts of...
3 min read
An array numArr[] that contains negative as well as non-negative elements of the size S. Also, the two other numbers, 'left' and 'right', is provided. Our task is to return the number of the sum of the range, such that the sum is lying between the...
12 min read
Multiplication of matrix is a core concept in programming. We can perform matrix multiplication in Java using a simple nested for loop approach to advance approach. The nested for loop approach has a time complexity of O(n3). The time complexity of matrix multiplication can be improved...
17 min read
In this program, the given ternary tree will be converted into a corresponding doubly linked list. The ternary tree is a hierarchical data structure in which each node can have at most three children. This can be accomplished by traversing the ternary tree in a pre-order...
6 min read
An array numArr[] is given, which only contains the non-negative integers, and an integer sp is also provided. The task is to partition the array into sp continuous subarrays such that the largest sum among these sp subarrays is minimum. Note that subarrays can never be...
19 min read
Write a code to prove that strings are immutable in java? Following program demonstrate it. File: ProveStringImmutable .java public class ProveStringImmutable { public static void referenceCheck(Object x, Object y) { if (x == y) { System.out.println("Both pointing to the same reference"); } else { System.out.println("Both are pointing to different reference"); } } public static void...
1 min read
In this section, we will learn what is tech number and how can we find tech numbers through a Java program. Tech Number A number is called a tech number if the given number has an even number of digits and the number can be divided exactly into...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India