, , java.util.LinkedList java.util.Stack, , , :
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(0);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
LinkedList<Integer> linkedList = new LinkedList<Integer>();
convertStackToLinkedList(stack, linkedList);
System.out.println("linkedList: "+linkedList);
}
public static void convertStackToLinkedList(Stack<Integer> stack, LinkedList<Integer> linkedList){
int topStackElement = stack.pop();
linkedList.add(0,topStackElement);
if(!stack.isEmpty())
convertStackToLinkedList(stack, linkedList);
}
, java.util.LinkedList, . , , add(int index, E element) java.util.LinkedList, . , , .
EDIT:
, Harsh Gupta , , StackOverflowError, , , , . - , , .