题解 | #链表的回文结构#

链表的回文结构

https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa

import java.util.*;

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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class PalindromeList {
    public boolean chkPalindrome(ListNode head) {
        // write code here
        ListNode slow=head;
        ListNode fast=head;
        if(head==null){
            return false;
        }
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode curN=slow.next;
        while(curN!=null){
            ListNode tmp=curN.next;
            curN.next=slow;
            slow=curN;
             curN=tmp;
           
        }
        curN=head;
        while(curN!=slow){
            if(curN.val!=slow.val){
                return false;
            }
            if(curN.next==slow){
                return true;
            }
            curN=curN.next;
            slow=slow.next;
        }
        return true;



    }
}

全部评论

相关推荐

码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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