-
Notifications
You must be signed in to change notification settings - Fork 10
/
IsPalindrome.java
55 lines (46 loc) · 1.29 KB
/
IsPalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.camnter.basicexercises.linklist;
import com.camnter.basicexercises.core.Node;
import java.util.Stack;
/**
* 链表是否是回文结构
* <p/>
* true 1 2 1
* true 1 2 2 1
* true 15 6 15
* false 1 2 3
* <p/>
* 回文:正走 和 反走,值都一样
* <p/>
* 用栈解,因为出栈正好相反
*
* @author CaMnter
*/
public class IsPalindrome<T extends Comparable<T>> {
public boolean isPalindrome(Node<T> head) {
Node<T> cur = head;
// 入栈
Stack<Node<T>> stack = new Stack<Node<T>>();
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
/**
* stack 控制 反走,出栈
* head 控制 正走
*/
while (!stack.isEmpty()) {
// 正反同时走的时候,不相等
if (head.value.compareTo(stack.pop().value) != 0) {
return false;
}
// 正走
head = head.next;
}
return true;
}
public static void main(String[] args) {
IsPalindrome<Integer> isPalindrome = new IsPalindrome<Integer>();
System.out.println(isPalindrome.isPalindrome(Node.getPalindromeLinkList()));
System.out.println(isPalindrome.isPalindrome(Node.getLinkList()));
}
}