Question

write the java code to convert a linked list of integers, to a stack of integers....

write the java code to convert a linked list of integers, to a stack of integers. thus your code will traverse the linked listand populate the stsck.

linked list 1->2->3. becomes stack 3
First Last 2
1
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//TestCode.java
import java.util.LinkedList;
import java.util.Stack;
public class TestCode {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        Stack<Integer> stack = new Stack<>();

        // Populating list with numbers 1,2,3
        list.add(1);
        list.add(2);
        list.add(3);

        // converting linked to stack
        for(int i = 0;i<list.size();i++){
            stack.push(list.get(i));
        }

        // Printing content of stack
        while (!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }
}

Output:

NW Process finished with exit code o

Add a comment
Know the answer?
Add Answer to:
write the java code to convert a linked list of integers, to a stack of integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT