234. 回文链表 + 翻转链表 + 回文判断 + 快慢指针

题目来源

LeetCode_234_回文链表

题目描述

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

示例 1:

输入: head = [1,2,2,1]
输出: true

示例 2:

输入: head = [1,2]
输出: false

提示:

  • 链表中节点数目在范围[1, 105]
  • 0 <= Node.val <= 9

进阶: 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

题解分析

解法一:先翻转再比较

  1. 本题考查的有两个知识点,一个是回文串的判断,另一个是快慢指针以及翻转链表。
  2. 首先需要使用快慢指针找到前后两部分,然后将后部分进行翻转。
  3. 最后再判断是否是回文的。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head;
        ListNode pre = null;
        while(fast != null){//找到前半部分的尾结点
            if(fast.next == null)
                break;
            fast = fast.next;
            pre = slow;
            slow = slow.next;
            fast = fast.next;
        }
        //翻转后半部分
        ListNode after = reverseLink(slow);
        if(pre != null)
            pre.next = null;
        while(head != null && after != null){
            if(head.val != after.val)
                return false;
            
            head = head.next;
            after = after.next;
        }
        return true;
    }
    public ListNode reverseLink(ListNode head){
        ListNode current = head;
        ListNode pre = null;
        while(current != null){
            ListNode temp = current.next;
            current.next = pre;
            pre = current;
            current = temp;
        }
        return pre;
    }
}

解法二:边遍历边翻转

  1. 解法一虽然比较简单,但是步骤较多,而且比较复杂。而且,从解法一的寻找中间节点中我们可以受到启发,能否不翻转后半链表,而是在快慢指针寻找中间节点的过程中反转前半部分链表呢?
  2. 根据上述的思想,我们可以设置一个pre节点,用来标识slow指针的前一个指针,并且帮助翻转前半部分链表。
  3. 这种解法需要注意的一点是,如果链表节点是奇数个的,那后半部分的开始节点将不是slow了,因为中间的那个节点不需要再考虑,所以这里需要增加一个额外的判断来跳过中间节点。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null){
            return true;
        }
        ListNode pre = null;
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null){
            ListNode slowNext = slow.next;
            fast = fast.next.next;
            slow.next = pre;
            pre = slow;
            slow = slowNext;
        }
        if(fast != null){// 这里很重要,这里是避免出现奇数个节点的链表,中间的那个节点不需要考虑,应该跳过
            slow = slow.next;
        }
        while(pre != null && slow != null){
            if(pre.val != slow.val){
                return false;
            }
            pre = pre.next;
            slow = slow.next;
        }
        return true;
    }
}

结果展示

image

posted @ 2021-03-22 10:56  Garrett_Wale  阅读(100)  评论(0编辑  收藏  举报