求链表倒数第k个元素

public static void getKthNode(LinkedNode node, int kth){
    	LinkedNode nodetmp = node;
    	int k = 0;
        while(node.next!=null){
        	if(k < kth){
        		k++;
        	} else {
        		nodetmp = nodetmp.next;
        	}
        	
        	node = node.next;
        }
    }


 public static MyNode mirrorTree1(MyNode node){
    	MyNode mirrorNode;
    	Stack<MyNode> stack = new Stack<MyNode>();
    	stack.add(node);
    	while(!stack.isEmpty()){
    		MyNode tmpNode = stack.pop();
    		mirrorNode = tmpNode.right;
    		tmpNode.right = tmpNode.left;
    		tmpNode.left = mirrorNode;
    		if(tmpNode.left != null){
    		    stack.push(tmpNode.left);
    		}
    		if(tmpNode.right != null){
    		    stack.push(tmpNode.right);
    		}
    	}
    	
    	
    	return node;
    }
    



你可能感兴趣的:(求链表倒数第k个元素)