首页 > 其他 > 详细

234 回文链表

时间:2019-01-12 18:55:22      阅读:227      评论:0      收藏:0      [点我收藏+]
ListNode *reverse(ListNode *head) {
    ListNode *front = head, *rear = nullptr, *temp = nullptr;
    while (front != nullptr) {
        temp = front->next;
        front->next = rear;
        rear = front;
        front = temp;
    }
    return rear;
}

ListNode *middleNode(ListNode *head) {
    ListNode *slow = head;
    ListNode *fast = head;
    while (fast != nullptr && fast->next != nullptr) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return fast == nullptr ? slow : slow->next;
}

bool isPalindrome(ListNode *head) {
    ListNode *mid = middleNode(head);
    ListNode *re = reverse(mid);
    while (re != nullptr) {
        if (re->val != head->val)
            return false;
        re = re->next;
        head = head->next;
    }
    return true;
}

234 回文链表

原文:https://www.cnblogs.com/INnoVationv2/p/10260558.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!