LinkedList:检查链表是否回文

2016-05-18  本文已影响0人  敲一手烂代码
public static boolean test5(Node node) {
        Stack<Integer> stack = new Stack<Integer>();
        Node head = node;
        while (head!=null) {
            stack.push(head.value);
            head = head.next;
        }
        
        while (!stack.isEmpty()) {
            if (stack.pop()!=node.value) {
                return false;
            }
            node = node.next;
        }
        
        return true;
    }
上一篇 下一篇

猜你喜欢

热点阅读