From 6e29e3721d89207a23713b0ed9f3833b56e2dd94 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 7 Jun 2021 13:21:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2002.06:=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/PalindromeLinkedListLcci.java | 69 +++++++++++++++++++ .../editor/cn/PalindromeLinkedListLcci.md | 21 ++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.java create mode 100644 src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.md diff --git a/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.java b/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.java new file mode 100644 index 0000000..536f388 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.java @@ -0,0 +1,69 @@ +//编写一个函数,检查输入的链表是否是回文的。 +// +// +// +// 示例 1: +// +// 输入: 1->2 +//输出: false +// +// +// 示例 2: +// +// 输入: 1->2->2->1 +//输出: true +// +// +// +// +// 进阶: +//你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题? +// Related Topics 链表 +// 👍 66 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//面试题 02.06:回文链表 +public class PalindromeLinkedListLcci{ + public static void main(String[] args) { + //测试代码 + Solution solution = new PalindromeLinkedListLcci().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + ListNode temp; + + 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; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.md b/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.md new file mode 100644 index 0000000..e5dce8f --- /dev/null +++ b/src/main/java/leetcode/editor/cn/PalindromeLinkedListLcci.md @@ -0,0 +1,21 @@ +

编写一个函数,检查输入的链表是否是回文的。

+ +

 

+ +

示例 1:

+ +
输入: 1->2
+输出: false 
+
+ +

示例 2:

+ +
输入: 1->2->2->1
+输出: true 
+
+ +

 

+ +

进阶:
+你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

+
Related Topics
  • 链表
  • \n
  • 👍 66
  • 👎 0
  • \ No newline at end of file