您的位置:首页 > 其它

LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

2016-02-02 12:33 465 查看

翻译

[code]给定一个单链表,确定它是否是回文的。

跟进:
你可以在O(n)时间和O(1)空间下完成它吗?


原文

[code]Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?


进阶

[code]bool judge(ListNode *head, ListNode* &cur) {
    if (!head)
        return true;
    if (!judge(head->next, cur))
        return false;
    if (cur->val != head->val)
        return false;
    else {
        cur = cur->next;
        return true;
    }
}

bool isPalindrome(ListNode* head) {
    ListNode *cur = head;
    return judge(head, cur);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: