-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathreverseLinkedList.java
More file actions
33 lines (29 loc) · 1.04 KB
/
reverseLinkedList.java
File metadata and controls
33 lines (29 loc) · 1.04 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
32
33
import java.util.LinkedList;
import java.util.Collections;
class main{
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of elements you want in your linked list: ");
int a=sc.nextInt();
int b;
System.out.print("Enter the elements in your linked list: ");
for(int i=0;i<a;i++){
b=sc.nextInt();
linkedList.add(b);
}
System.out.println();
System.out.println("The elements after traversing the linked list in reverse are: ");
ListIterator<Integer> listIterator= linkedList.listIterator();
for(int i=0;i<a;i++){
if(listIterator.hasNext()){
stack.push(listIterator.next());
}
}
for(int i=0;i<a;i++){
System.out.print(stack.pop()+" ");
}
System.out.println();
}
}