题目:给定一个链表的头节点head,请判断该链表是否为回文结构(正反一样)。
例如:1->2->1,返回true。
1->2->2->1,返回true。
15->6->15,返回true。
1->2->3,返回false。
进阶:如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1)
分析:基本解法:如果有一个链表1->2->2->1,准备一个栈,遍历它的时候,把节点压到栈里面去,由于栈是先进后出,则从栈顶到栈底其实是原来链表顺序的逆序。再从头遍历链表,每遍历一个节点,从栈里面取出一个数,比对相等还是不相等。相当于原始顺序和逆序依次进行比较。如果比对到最后每一步都相等,则是回文结构。如果有任何一步对不上,就不是回文的。(这种做法额外空间复杂度是O(N))。
稍好解法:设置两个指针,一个叫快指针,一个叫慢指针,快指针一次走两步,慢指针一次走一步。快指针走完的时候,慢指针会来到中点的位置。(所以要在不用额外空间的情况下,找到链表的中点位置。)然后将右半部分压到栈里去,相当于栈里面是链表后半部分的逆序。然后链表的前半部分和栈里面的数对比,如果每一步都能对上,就是回文结构。实质就是,假设有一段线,从中点开始,将右半部分折回去,与前半部分对比。(这种做法额外空间复杂度还是O(N),但实际结果是省了一半空间)。
进阶解法:快指针一次走两步,慢指针一次走一步,快指针走完的时候,慢指针会来到中点的位置。从中点开始,让右半部分开始逆序(意思是比如一个链表1->2->3->2->1,后半部分逆序就变成是1->2->3<-2<-1结构),然后两头开始遍历,中途有任何一个对不上,就不是回文结构。如果全都对上,就是回文结构。但要记得不管是不是回文,都要在返回结构之前,把链表结构调回来。(额外空间复杂度达到O(1))
import java.util.Stack;
public class IsPalindromeList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
// need n extra space
public static boolean isPalindrome1(Node head) {
Stack<Node> stack = new Stack<Node>();
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;
}
// need n/2 extra space
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<Node>();
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;
}
// need O(1) extra space
public static boolean isPalindrome3(Node head) {
if (head == null || head.next == null) {
return true;
}
Node slow = head;
Node fast = head;
while (fast.next != null && fast.next.next != null) { // find mid node
slow = slow.next; // n1 -> mid
fast = fast.next.next; // n2 -> end
}
fast = slow.next; // n2 -> right part first node
slow.next = null; // mid.next -> null
Node n3 = null;
//右半部分逆序
while (fast != null) { // right part convert
n3 = fast.next; // n3 -> save next node
fast.next = slow; // next of right node convert
slow = fast; // n1 move
fast = n3; // n2 move
}
n3 = slow; // n3 -> save last node
fast = head;// n2 -> left first node
boolean res = true;
while (slow != null && fast != null) { // check palindrome
if (slow.value != fast.value) {
res = false;
break;
}
slow = slow.next; // left to mid
fast = fast.next; // right to mid
}
slow = n3.next;
n3.next = null;
while (slow != null) { // recover list
fast = slow.next;
slow.next = n3;
n3 = slow;
slow = fast;
}
return res;
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(2);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
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);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
}
}