-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava_1_5_2.java
87 lines (80 loc) · 2.94 KB
/
java_1_5_2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package chapter1;
import base.ListNode;
/**
* 判断单向链表是不是回文串
*/
public class java_1_5_2 {
public static void main(String[] argv) {
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1, 2, 3, 4})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1,1})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1,2,1})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1,2,2,1})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1,2,3,2,1})));
System.out.println(isPalindrome1LinkList(createLinkList(new int[]{1,2,3,4,2,1})));
ListNode node = createLinkList(new int[]{1,2,3,3,2,1});
System.out.println(isPalindrome1LinkList(node));
}
//先快慢指针让慢指针走到链表的正中间,然后反转后半部分链表。然后分别从链表头和中间出发进行回文比对
//1->2->3->3->2->1 => 1->2->3->1->2->3
public static boolean isPalindrome1LinkList(ListNode head) {
if (head == null || head.nextNode == null) {
return true;
}
ListNode slow = head;
ListNode fast = head.nextNode;
while (fast.nextNode != null) {
slow = slow.nextNode;
fast = fast.nextNode;
if (fast.nextNode == null) {
break;
}
fast = fast.nextNode;
}
slow.nextNode = reverseLinkList(slow.nextNode);
ListNode firstHead = head;
ListNode secondHead = slow.nextNode;
while (secondHead != null) {
if (firstHead.value != secondHead.value) {
return false;
}
firstHead = firstHead.nextNode;
secondHead = secondHead.nextNode;
}
return true;
}
private static ListNode reverseLinkList(ListNode head) {
if (head == null || head.nextNode == null) {
return head;
}
ListNode pre = head;
ListNode next = head.nextNode;
pre.nextNode = null;
while (next != null) {
ListNode tmp = next.nextNode;
next.nextNode = pre;
pre = next;
next = tmp;
}
return pre;
}
private static ListNode createLinkList(int[] data) {
ListNode head = new ListNode(data[0]);
ListNode node = head;
for (int i = 1; i < data.length; i++) {
ListNode newNode = new ListNode(data[i]);
node.nextNode = newNode;
node = newNode;
}
return head;
}
private static void printLinkList(ListNode head) {
System.out.println("链表为");
ListNode node = head;
while (node != null) {
System.out.print(node.value + "->");
node = node.nextNode;
}
System.out.println("null");
}
}