Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My first commmit #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src-java/reverseLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}