java stack stack of application scenarios

Stack features: last out 

1. Single chain, a reciprocal of the number of K

public class MyNode {
    public Integer value;
    public MyNode next;
    public MyNode(Integer value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "MyNode{" +
                "value=" + value +
                ", next=" + next +
                '}';
    }
}
 MyNode node=new MyNode(1);
        MyNode temp=node;
        for(int i=2;i<11;i++){
            MyNode nodeNext=new MyNode(i);
            temp.next=nodeNext;
            temp=nodeNext;
        }
       int val= getSingleListLastKNum(node,4);
        System.out.println(val);
 public static int getSingleListLastKNum(MyNode node,int k){
        Stack<MyNode> stacks=new Stack<>();
        while(node!=null){
            stacks.push(node);
            node=node.next;
        }
        MyNode target=null;
        int size=stacks.size();
        while(stacks.size()>0){
            target=stacks.pop();
            int subSize=stacks.size();
            if(size-subSize==k){
                break;
            }
        }
       return target.value;
    }

The idea is to stack into the list and then removed from the stack of K elements of K that is the reciprocal of the number.

This is the optimal solution to the question of double pointer, link here: https://www.cnblogs.com/edisonchou/p/4769164.html

2. The single list, determine whether it is a palindrome

 MyNode node=new MyNode(1);
        MyNode temp=node;
        int j=1;
        for(int i=2;i<10;i++){
            int val=i;
            if(i>5){
                val=i-2*j;
                j++;
            }
            MyNode nodeNext=new MyNode(val);
            temp.next=nodeNext;
            temp=nodeNext;
        }
        System.out.println(node);
     boolean flag=isPalindromeNum(node);
        System.out.println(flag);
 public static boolean isPalindromeNum(MyNode node){
        boolean flag=true;
        MyNode originalNode=node;
        Stack<MyNode> stacks=new Stack<>();
        while(node!=null){
            stacks.push(node);
            node=node.next;
        }
        while(stacks.size()>0){
           Integer value=stacks.pop().value;
           Integer oriVal=originalNode.value;
           if(value!=oriVal){
               flag=false;
               break;
           }
           originalNode=originalNode.next;
        }
        return flag;
    }

The idea is to stack into the list and then removed from the stack elements sequentially compared with the value of the node in the linked list, if a number is not equal out directly, not described palindrome number, otherwise palindrome.

This problem of the optimal solution is the speed of the pointer, link here:  https://blog.csdn.net/whm114336793/article/details/79996943

 

to sum up:

     According to the characteristics of the stack, advanced after we found it convenient evaluation or judgment after a common feature of these two questions is required to reverse a linked list. Therefore, the stack is the need to use object or scene arbitrary data after descending as a type of logic processing, for example: reverse output, so are the XML grammar checker meet this scenario

Published 25 original articles · won praise 51 · views 20000 +

Guess you like

Origin blog.csdn.net/Royal_lr/article/details/88734320