题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
http://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
快慢指正
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode slow = head;
ListNode fast = head.next;
ListNode p = null;
while(fast != null && fast.next != null) {
ListNode t = slow.next;
slow.next = p;
p = slow;
slow = t;
fast = fast.next.next;
}
ListNode right = slow.next;
slow.next = p;
if (fast == null) {
slow = slow.next;
}
while(right != null) {
if (right.val != slow.val) {
return false;
}
right = right.next;
slow = slow.next;
}
return true;
}
}

字节跳动公司福利 1371人发布