力扣:面试题 02.06. 回文链表

This commit is contained in:
huangge1199 2021-02-08 13:36:49 +08:00
parent ea8cf14329
commit ba81f7bcd9

View File

@ -0,0 +1,33 @@
package com.code.leet.study.t20210208;
import com.code.leet.entiy.ListNode;
/**
* 编写一个函数检查输入的链表是否是回文的
*/
public class IsPalindrome {
ListNode temp;
/**
* 面试题 02.06. 回文链表
*/
public boolean isPalindrome(ListNode head) {
temp = head;
return isP(head);
}
public boolean isP(ListNode curNode) {
if (curNode != null) {
if (isP(curNode.next)) {
if (curNode.val != temp.val) {
return false;
}
temp = temp.next;
return true;
} else {
return false;
}
}
return true;
}
}