难度简单762
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
解题思路
快慢指针的话就是每次快指针走两步,慢指针走一步,这里要注意循环的条件。当走完后,slow会在中点的位置。要根据节点的奇偶,来决定中点的位置
作者:jasscical
链接:https://leetcode-cn.com/problems/palindrome-linked-list/solution/234hui-wen-lian-biao-kuai-man-zhi-zhen-zhan-shu-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/**快慢指针法
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseListNode(ListNode* node){ //反转链表
ListNode* pre = nullptr;
ListNode* cur = node;
while(cur){
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
bool isPalindrome(ListNode* head) {
if(head == nullptr || head->next == nullptr) return true;
ListNode* slow = head;
ListNode* fast = head;
ListNode* pre = slow;
int cnt = 0;
ListNode* tmp = head;
while(tmp){
tmp = tmp->next;
++cnt;
}
while(fast && fast->next){
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = nullptr; //前半部分断连
if(cnt % 2) slow = slow->next; //如果链表是奇数个节点,中点需要往后移动一位
//反转后半部链表
ListNode* cur1 = head;
ListNode* cur2 = slow;
ListNode* r_cur2 = reverseListNode(cur2);
while(cur1){
if(cur1->val != r_cur2->val) return false;
cur1 = cur1->next;
r_cur2 = r_cur2->next;
}
return true;
}
};
/**栈1,比较一半节点
* 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 == nullptr || head->next == nullptr) return true;
stack<int> st;
ListNode* cur = head;
int cnt = 0;
while(cur){
st.push(cur->val);
cur = cur->next;
++cnt; //记录链表节点个数
}
cur = head;
cnt /= 2; //比较一半就行
while(cnt--){
int node_val = st.top();
st.pop();
if(node_val != cur->val) return false;
else cur = cur->next;
}
return true;
}
};
作者:jasscical
链接:https://leetcode-cn.com/problems/palindrome-linked-list/solution/234hui-wen-lian-biao-kuai-man-zhi-zhen-zhan-shu-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/**栈2 比较全部链表节点
* 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 == nullptr || head->next == nullptr) return true;
stack<int> st;
ListNode* cur = head;
while(cur){
st.push(cur->val);
cur = cur->next;
}
cur = head;
while(!st.empty() && cur){
int node_val = st.top();
st.pop();
if(node_val != cur->val) return false;
else cur = cur->next;
}
return true;
}
};
作者:jasscical
链接:https://leetcode-cn.com/problems/palindrome-linked-list/solution/234hui-wen-lian-biao-kuai-man-zhi-zhen-zhan-shu-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/**数组
* 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;
vector<int> ivec1;
while(head){
ivec1.push_back(head->val);
head = head->next;
}
vector<int> ivec2 = ivec1;
reverse(ivec1.begin(), ivec1.end()); //反转数组
if(ivec1 == ivec2) return true;
return false;
}
};
作者:jasscical
链接:https://leetcode-cn.com/problems/palindrome-linked-list/solution/234hui-wen-lian-biao-kuai-man-zhi-zhen-zhan-shu-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。