判断一个链表是否为回文结构

2019-11-05  本文已影响0人  Ramsey16k

【题目】 给定一个链表的头节点head,请判断该链表是否为回
文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。
15->6->15,返回true。 1->2->3,返回false。
进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂
度达到O(1)。

解题思路:

(1)第一种实现方式:额外辅助空间为n

需要另开一个大小为n的栈,把链表的所有节点压入栈中。然后开始弹栈,栈顶元素就相当于是链表的最后一个元素,每次出栈一个值,其实就相当于从后往前遍历这个链表;与此同时,将链表从头结点开始向后遍历。
然后比较出栈元素和链表当前元素是否相等,如果不相等就返回false。
当整个链表遍历完毕/所有元素都出栈时,头节点为空,说明这个链表是回文的,返回true。

public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }
public static boolean isPalindrome1(Node head) {
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (head != null) {
            if (head.value != stack.pop().value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

(2)第二种实现方式:额外辅助空间为n/2

需要另开一个大小为n/2的栈。用一个慢指针和一个快指针。快指针每次向前移动2步,慢指针每次向前移动1步。当快指针走到头时,慢指针指向的位置就是链表的中点。
然后把链表的后半段压入栈中,之后开始弹栈,比对前半段和后半段,和第一种实现方式同理。

public static boolean isPalindrome2(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Node right = head.next; // 快指针
        Node cur = head; // 慢指针
        while (cur.next != null && cur.next.next != null) {
            right = right.next;
            cur = cur.next.next;
        }
        Stack<Node> stack = new Stack<>();
        while (right != null) {
            stack.push(right);
            right = right.next;
        }
        while (!stack.isEmpty()) {
            if (head.value != stack.pop().value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

(3)第三种实现方式:额外辅助空间为O(1)

把后半段的链表反转,然后和前半段进行比较。
链表长度为奇数时,慢指针来到中点位置,偶数时来到两个中点中的前一个位置。

public static boolean isPalindrome3(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Node n1 = head;
        Node n2 = head;
        while (n2.next != null && n2.next.next != null) { // 寻找中点
            n1 = n1.next; // n1 -> 中间节点
            n2 = n2.next.next; // n2 -> 尾节点
        }
        n2 = n1.next; // n2 -> 右边的第一个节点
        n1.next = null; // 中点的下一个节点 -> null
        Node n3;
        while (n2 != null) { // right part convert
            n3 = n2.next; // n3 -> 存n2的下一个节点
            n2.next = n1; // n2(右边的第一个节点)的下一个节点 -> n1(中点)
            n1 = n2; // n1 -> 右边第一个节点
            n2 = n3; // n2 -> 右边第一个节点的下一个节点
        }
        n3 = n1; // n3 -> 存最后一个节点
        n2 = head; // n2 -> 左边的第一个节点
        boolean res = true;
        while (n1 != null && n2 != null) { // 检查是否为回文结构
            if (n1.value != n2.value) {
                res = false;
                break;
            }
            n1 = n1.next; // 头节点遍历到中点
            n2 = n2.next; // 尾节点遍历到中点
        }
        n1 = n3.next;
        n3.next = null;
        while (n1 != null) { // 恢复链表
            n2 = n1.next;
            n1.next = n3;
            n3 = n1;
            n1 = n2;
        }
        return res;
    }
上一篇 下一篇

猜你喜欢

热点阅读