题目:
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?
题意:
给定一个单链表,检测其是否是回文链表
Follow up:
是否做到时间复杂度为O(n),空间复杂度为O(1);
思路:
1、遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回文数组,时间复杂度为O(n),但空间复杂度也为O(n),不满足空间复杂度要求。
2、利用栈先进后出的性质,将链表前半段压入栈中,再逐个弹出与链表后半段比较。时间复杂度O(n),但仍然需要n/2的栈空间,空间复杂度为O(n)。
3、反转链表法,将链表后半段原地翻转,再将前半段、后半段依次比较,判断是否相等,时间复杂度O(n),空间复杂度为O(1)满足题目要求。
代码:C++版:28ms
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool isPalindrome(ListNode* head) { if(head==NULL || head->next==NULL){ return true; } ListNode* slow = head; ListNode* fast = head; while(fast->next!=NULL && fast->next->next!=NULL){ slow = slow->next; fast = fast->next->next; } slow->next = reverseList(slow->next); slow = slow->next; while(slow!=NULL){ if(head->val!=slow->val){ return false; } head = head->next; slow = slow->next; } return true; } ListNode* reverseList(ListNode* head){ ListNode* pre = NULL; ListNode* next = NULL; while(head!=NULL){ next = head->next; head->next = pre; pre = head; head = next; } return pre; } };
代码:C++版:28ms
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool isPalindrome(ListNode* head) { //如果链表为空或者仅有一个元素那么肯定是回文链表 if (!head || !head->next) { return true; } //快慢指针法,寻找链表中心 ListNode * slow, *fast; slow = fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } if (fast) { //链表元素奇数个 slow->next = reverseList(slow->next); slow = slow->next; }else{ //链表元素偶数个 slow = reverseList(slow); } while (slow) { if (head->val != slow->val) { return false; } slow = slow->next; head = head->next; } return true; } ListNode* reverseList(ListNode* head){ ListNode* pre = NULL; ListNode* next = NULL; while(head!=NULL){ next = head->next; head->next = pre; pre = head; head = next; } return pre; } };