Java program to rotate doubly linked list by N nodes17 Mar 2025 | 4 min read In this program, we need to create a doubly linked list and rotate it by n node. This can be achieved by maintaining a pointer that starts from the head node and traverses the list until current points to the nth node. Move the list from head to the nth node and place it after tail. Now nth node will be the tail of the list and node next to nth node will be the new head. Here, n should always be greater than 0 but less than the size of the list. Original List: ![]() List after rotating it by 3 nodes: ![]() In the above example, we need to rotate list by 3 nodes. First, we iterate through the list until current points to the 3rd node which is, in this case, are node 3. Move the list from node 1 to 3 and place it after tail. Now, node 4 will be new head and node 3 will be the new tail. Algorithm
a. rotateList() will rotate the list by given n nodes.
a. display() will show all the nodes present in the list.
Program:Output: Original List: 1 2 3 4 5 Updated List: 4 5 1 2 3 Next TopicJava Programs |
It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going...
3 min read
Java Program to find Largest Number in an Array We can find the largest number in an array in java by sorting the array and returning the largest number. Let's see the full example to find the largest number in java array. public class LargestInArrayExample{ public static int getLargest(int[]...
2 min read
In this program, we need to create a singly linked list and display the list in reverse order. Original List Reversed List One of the approaches to solving this problem is to reach the end the of the list and display the nodes from tail to head recursively. Algorithm Create...
5 min read
Java Program to print Odd and Even Numbers from an Array We can print odd and even numbers from an array in java by getting remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is...
1 min read
In this section, we will learn automorphic numbers with examples and also create Java programs that check whether the number is automorphic or not. What is an automorphic number? A number is called an automorphic number if and only if the square of the given number ends with...
3 min read
In this section, we will learn what is a smith number and also create Java programs to check if the given number is smith or not. The smith number program frequently asked in Java coding tests and academics. Smith Number A Smith number is a composite number whose...
4 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, HCL, IBM, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
4 min read
Bubble Sort is one of the simplest sorting algorithms, primarily used for educational purposes due to its ease of implementation and understanding. Despite its simplicity, it's not the most efficient sorting algorithm for large datasets. However, comprehending Bubble Sort provides a foundational understanding of sorting...
6 min read
Java Program to Print Spiral Pattern The spiral pattern (or matrix in spiral form) is frequently asked in Java interviews and academics. In this section, we will create a Java program to create a spiral pattern or spiral matrix. What is Spiral Matrix or Spiral Pattern? A spiral...
7 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
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