From ba81f7bcd96acce47cbe0759c8b72852a4d03855 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 8 Feb 2021 13:36:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=98=2002.06.=20=E5=9B=9E=E6=96=87=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leet/study/t20210208/IsPalindrome.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 LeetCode/src/main/java/com/code/leet/study/t20210208/IsPalindrome.java diff --git a/LeetCode/src/main/java/com/code/leet/study/t20210208/IsPalindrome.java b/LeetCode/src/main/java/com/code/leet/study/t20210208/IsPalindrome.java new file mode 100644 index 0000000..f1e724b --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/study/t20210208/IsPalindrome.java @@ -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; + } +}