3、rns;}}面试题5:输入一个链表,从尾到头打印链表每个节点的值。P51/***publicclassListNode{*intval;*ListNodenext=null;**ListNode(intval){*this.val=val;*}*}**/importjava.util.ArrayList;importjava.util.Stack;publicclassSolution{publicArrayListprintListFromTailToHead(ListNodelistNode){Stacksta
4、ck=newStack();ArrayListlist=newArrayList();//新生成的从后到前的链ListNodecurrent=listNode;while(current!=null){stack.push(current);current=current.next;}while(!stack.isEmpty()){list.add(newInteger(stack.pop().val));}returnlist;}}面试题6:重建二叉树/***Definitionforbinar
5、ytree*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(intx){val=x;}*}*/publicclassSolution{publicTreeNodereConstructBinaryTree(int[]pre,int[]in){//pre前序in中序TreeNoderoot=reConstruct(pre,0,pre.length-1,in,0,in.length-1);returnroot;}//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列
6、{4,7,2,1,5,3,8,6}privateTreeNodereConstruct(int[]pre,intstartPre,intendPre,int[]in,intstartIn,intendIn){if(startPre>endPre
7、
8、startIn>endIn)returnnull;TreeNoderoot=newTreeNode(pre[startPre]);for(inti=startIn;i<=endIn;i++)if(in[i]==pre[startPre]){root.left=reConstruct(pre,startPre+
9、1,startPre+i-startIn,in,startIn,i-1);root.right=reConstruct(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);}returnroot;}}面试题7:用两个栈实现队列P59importjava.util.Stack;publicclassSolution{Stackstack1=newStack();Stackstack2=newStack();publicvoidpush(intn
10、ode){stack1.push(newInteger(node));}publicintpo