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

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

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

将链表值存入数组,双指针判断是否为回文结构
public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
        ListNode temp = head;
        int len = 0;
        while(temp != null){
            len++;
            temp = temp.next;
        }
                // 创建和链表长度相同的数组,并将链表值存入数组
        int[] res = new int[len];
        for(int i = 0; i < len; i++){
            res[i] = head.val;
            head = head.next;
        }
                // 双指针判断是否是回文结构
        int left = 0, right = len - 1;
        while(left < right){
            if(res[left] != res[right]){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}                


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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