判断一个链表是不是回文结构

方法一:将整个链表入栈

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Stack;

class Node{
    public int val;
    public Node next;
    public Node(int val){
        this.val = val;
    }
}

public class Main{
    public static boolean isCircle(Node head) {
        if (head == null || head.next == null) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        Node cur = head;
        while (cur !=null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        while(!stack.isEmpty()){
            int val = stack.pop();
            if(val != head.val){
                return false;
            }
            head = head.next;
        }
        return true;
    }
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] elements = bf.readLine().split(" ");
        Node head = new Node(Integer.parseInt(elements[0]));
        Node node = head;
        for (int i = 1; i < elements.length; i++) {
            head.next = new Node(Integer.parseInt(elements[i]));
        }
        System.out.println(isCircle(head));
    }
}

方法二:将一半链表入栈

    public static boolean isPalindrome(Node head){
        if (head == null || head.next == null){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        Node cur = head.next;
        Node fast = head;
        while(fast.next != null && fast.next.next != null){
            cur = cur.next;
            fast = fast.next.next;
        }
        while(cur != null){
            stack.push(cur.val);
            cur = cur.next;
        }
        while(!stack.isEmpty()){
            int val = stack.pop();
            if (head.val != val){
                return false;
            }
            head = head.next;
        }
        return true;
    }

方法三:利用三个指针,不用栈

这个方法的操作顺序是:

  1. 先反转后一半链表;
  2. 然后从链表两端开始向中间遍历,判断是否是回文;
  3. 将链表恢复为原来的样子。
    public static boolean isPalindrome(Node head){
        if (head == null || head.next == null){
            return false;
        }
        boolean res = true;
        Node node1 = head;
        Node node2 = head;
        while(node2.next != null && node2.next.next != null){
            node1 = node1.next;
            node2 = node2.next.next;
        }

        node2 = node1.next;
        node1.next = null;

        Node node3 = null;
        while (node2 != null){
            node3 = node2.next;
            node2.next = node1;
            node1 = node2;
            node2 = node3;
        }
        // node1 保存的是头节点,现在将其给node3
        node3 = node1;
        node2 = head;

        while (node1 != null && node2 != null){
            if (node1.val != node2.val){
                res = false;
                break;
            }
            node1 = node1.next;
            node2 = node2.next;
        }

        node2 = node3.next;
        node3.next = null;
        while (node2 != null){
            node1 = node2.next;
            node2.next = node3;
            node3 = node2;
            node2 = node1;
        }

        return res;
    }

你可能感兴趣的:(算法与数据结构)