找到中点前一个节点, 然后反转后一部分, 最后比较, 出现不同return false

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
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/**
* 找到中点前一个节点, 然后反转后一部分, 最后比较, 出现不同return false
*/
public boolean isPalindrome(ListNode head) {
ListNode mid = middleNode(head);
ListNode head2 = mid.next;
mid.next = null;
head2 = reverse(head2);
while (head != null && head2 != null) {
if (head.val != head2.val) {
return false;
}
head = head.next;
head2 = head2.next;
}
return true;
}

private ListNode reverse(ListNode head) {
ListNode cur = head, prev = null;
while (cur != null) {
ListNode nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
return prev;
}

private ListNode middleNode(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode slow = dummy, fast = dummy;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}

Comments
Recent Posts
进程树
进程管理
Categories
Website Info
Article Count :
3
Total Word Count :
4.7k
Unique Visitors :
Page Views :
Last Update :