-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0234.java
More file actions
66 lines (57 loc) · 1.98 KB
/
Copy pathLeetCode0234.java
File metadata and controls
66 lines (57 loc) · 1.98 KB
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
/* Palindrome Linked List
* Example:
* Input: 1->2->2->1
* Output: true
* */
public class LeetCode0234 {
public static void main(String args[]) {
int[] input = new int[]{1, 2, 3, 4, 2, 1};
ListNode head = buildListNode(input);
System.out.println(isPalindrome(head));
}
public static boolean isPalindrome(ListNode head) {
if (head == null || head.next == null)
return true;
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode middle = slow; //找到中点
ListNode reversed = slow; //从此处开始反转
ListNode toDo = slow.next; //要处理的ListNode
middle.next = null; //中点指向null
while (toDo != null) { //从中点开始反向,直到toDo为null(此时reversed为反向后的headListNode)
ListNode temp = toDo.next;
toDo.next = reversed;
reversed = toDo;
toDo = temp;
}
while (head != null && reversed != null) { //从头比较两个链表
if (head.val != reversed.val) {
return false;
}
head = head.next;
reversed = reversed.next;
}
return true;
}
private static ListNode buildListNode(int[] input) {
ListNode first = null, last = null, newNode;
if (input.length > 0) {
for (int i = 0; i < input.length; i++) {
newNode = new ListNode(input[i]);
newNode.next = null;
if (first == null) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
last = newNode;
}
}
}
return first;
}
}