forked from qiurunze123/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc234.java
43 lines (42 loc) · 1.21 KB
/
lc234.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
package code;
/*
* 234. Palindrome Linked List
* 题意:判断链表是否是回文的
* 难度:Easy
* 分类:LinkedList, Two Pointers
* 思路:反转一半就行了,避免了空间开销
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
*/
public class lc234 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public boolean isPalindrome(ListNode head) {
if(head==null||head.next==null)
return true;
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode head2 = reverse(slow.next,null);
slow.next = null; //注意截断一下
while(head2!=null&&head!=null){
if(head2.val!=head.val)
return false;
head = head.next;
head2 = head2.next;
}
return true;
}
public ListNode reverse(ListNode head, ListNode pre){
ListNode next = head.next;
head.next = pre;
if(next==null)
return head;
return reverse(next, head);
}
}