X Tutup
The Wayback Machine - https://web.archive.org/web/20210125044321/https://github.com/andavid/leetcode-java/issues/2
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如果是奇数个的回文的话判断是会不准吧 #2

Open
EthanHuntDeng opened this issue Dec 15, 2019 · 1 comment
Open

如果是奇数个的回文的话判断是会不准吧 #2

EthanHuntDeng opened this issue Dec 15, 2019 · 1 comment

Comments

@EthanHuntDeng
Copy link

@EthanHuntDeng EthanHuntDeng commented Dec 15, 2019

No description provided.

@EthanHuntDeng
Copy link
Author

@EthanHuntDeng EthanHuntDeng commented Dec 15, 2019

public static Boolean is(Node head){

    if(head == null || head.next == null){
        return true;
    }
    Node prev =  null;
    Node fast = head;
    Node slow = head;
    Boolean a = false;
    while (fast != null ){
        if(fast.next!=null){
            fast = fast.next.next;
        }else{
            fast = null;
            a = true;
        }

        Node node = slow.next;
        slow.next=prev;
        prev = slow;
        slow = node;
    }
    Node midalNode = slow;
    if(!a){
        midalNode = slow.next;
    }
    while (midalNode != null){
        if(!midalNode.s.equals(prev.s)){
            return false;
        }
        midalNode = midalNode.next;
        prev = prev.next;
    }
    return true;
}

这样写就没问题了,不管中间数是偶数还是奇数都可以搞定了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
X Tutup