-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightshift.java
More file actions
31 lines (27 loc) · 1.02 KB
/
Copy pathRightshift.java
File metadata and controls
31 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package day_12;
import java.util.*;
public class Rightshift {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements in the list: ");
int n = sc.nextInt();
System.out.print("Enter the number of positions to right shift: ");
int m = sc.nextInt();
LinkedList<Integer> linkedList = new LinkedList<>();
// Taking input from the user to fill the linked list
System.out.println("Enter the elements of the list:");
for (int i = 0; i < n; i++) {
linkedList.add(sc.nextInt());
}
// Perform right shift by m times
for (int i = 0; i < m; i++) {
int last = linkedList.removeLast();
linkedList.addFirst(last);
}
// Print the final linked list
System.out.println("List after right shift:");
for (int i : linkedList) {
System.out.println(i);
}
}
}