(链表_01)回文链表(快慢指针、栈、数组)

234. 回文链表

难度简单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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasscical

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值