题解 | #判断一个链表是否为回文结构#

判断一个链表是否为回文结构

https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
        //遍历后放入list集合
        List<Integer> list= new ArrayList(1000);
        while(head != null){
            list.add(head.val);
            head = head.next;
        }
        System.out.println("aa:"+(-401261 == -401261));
        // 双指针遍历list集合比较
        int  left = 0;
        int right = list.size()-1;
        while(list != null && left <= right) {
            System.out.print(list.get(left) +" == "+ list.get(right));
            if(!list.get(left).equals(list.get(right))) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务