您的位置:首页 > 其它

[leetCode] Palindrome Linked List

2015-08-19 00:56 411 查看
Given a singly linked list, determine if it is a palindrome.

Follow up:

Could you do it in O(n) time and O(1) space?
public class Solution {
ListNode h;
public boolean isPalindrome(ListNode head) {
if (head == null) return true;

if (h == null) h = head;

boolean tmp = true;
if (head.next != null) tmp &= isPalindrome(head.next);

tmp &= (head.val == h.val);
h = h.next;
return tmp;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息