题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
http://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
思路:遍历链表,把值存入list集合中。使用双指针,判断左右是否相等,不等则返回false。循环结束返回true。
public boolean isPail (ListNode head) {
        // write code here
        if(head==null){
            return false;
        }
        ArrayList<Integer> list=new ArrayList<Integer>();
        ListNode h=head;
        while(h!=null){
            list.add(h.val);
            h=h.next;
        }
        int l=0;
        int r=list.size()-1;
        while(l<r){
            if(!list.get(l).equals(list.get(r))){//此处我用==,有两个案例不通过。但案例确实不是回文体,我返回的false,答案却是true。
                return false;
            }
            l++;
            r--;
        }
        return true;
    } 科大讯飞公司氛围 425人发布
科大讯飞公司氛围 425人发布 查看10道真题和解析
查看10道真题和解析