数据结构与算法(JAVA版)6_2:回文问题

判断一个单链表结构的字符串是否是回文结构

package com.inspire.chapter6;

import java.util.Stack;

public class Code02_IsPalindromeList {

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

		public Node(int v) {
			value = v;
		}
	}

	// 方法一:将单链表中的结点按顺序压入栈中,压栈完毕后,将单链表中的每一个结点依次和栈中弹出的结点比较,相等则继续比较,否则返回false
	// 空间复杂度O(N)
	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;
	}

	// 升级——方法二:省空间
	/*
	 * 1.将slow定位到单链表的中点(奇数个)/上中点(偶数个) 
	 * 2.从slow.next结点开始依次压入栈中
	 * 3.从单链表头结点开始与栈中元素弹出的结点比较,直到走到slow结点或者中途匹配不一致返回false
	 */
	// 空间复杂度O(N/2)
	public static boolean selftest(Node head) {
		if (head.next == null) {// 如果只有一个结点
			return true;
		} else if ((head.next.next == null) && (head.value == head.next.value)) {// 如果有两个结点且值相等
			return true;
		}
		Node slow = head.next;
		Node fast = head.next.next;
		while (fast.next != null && fast.next.next != null) {
			slow = slow.next;
			fast = fast.next.next;
		} // 这样,slow就定位好了
			// System.out.println(slow.value);
		Node temp = slow.next;
		Stack<Node> stack = new Stack<>();
		while (temp != null) {
			stack.push(temp);
			temp = temp.next;
		}
		while (!stack.isEmpty()) {
			if (head.value != stack.pop().value) {
				return false;
			}
			head = head.next;
		}
		return true;
	}

	// 方法三——最优解:空间复杂度O(1)
	/*
	 * 1.将slow定位到单链表的中点(奇数个)/上中点(偶数个)
	 * 2.从slow.next开始依次反转 
	 * 3.首尾比较,相等则继续比较,否则break
	 * 4.将单链表重置到之前的状态
	 */
	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) { // find mid node
			n1 = n1.next; // n1 -> mid
			n2 = n2.next.next; // n2 -> end
		}
		n2 = n1.next; // n2 -> right part first node
		n1.next = null; // mid.next -> null
		Node n3 = null;
		while (n2 != null) { // right part convert
			n3 = n2.next; // n3 -> save next node
			n2.next = n1; // next of right node convert
			n1 = n2; // n1 move
			n2 = n3; // n2 move
		}
		n3 = n1; // n3 -> save last node
		n2 = head;// n2 -> left first node
		boolean res = true;
		while (n1 != null && n2 != null) { // check palindrome
			if (n1.value != n2.value) {
				res = false;
				break;
			}
			n1 = n1.next; // left to mid
			n2 = n2.next; // right to mid
		}
		n1 = n3.next;
		n3.next = null;
		while (n1 != null) { // recover list
			n2 = n1.next;
			n1.next = n3;
			n3 = n1;
			n1 = n2;
		}
		return res;
	}

	public static void main(String[] args) {
		Node head = null;
		head = new Node(1);
		head.next = new Node(2);
		head.next.next = new Node(3);
		head.next.next.next = new Node(2);
		head.next.next.next.next = new Node(1);
		// head.next.next.next.next.next = new Node(1);
		// head.next.next.next.next.next.next = new Node(1);
		System.out.println(isPalindrome1(head));
		System.out.println(selftest(head));
		System.out.println(isPalindrome3(head));
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章