數據結構與算法(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));
	}

}

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