面试题 02.06:回文链表

This commit is contained in:
huangge1199 2021-06-07 13:21:22 +08:00
parent 779e7f5487
commit 6e29e3721d
2 changed files with 90 additions and 0 deletions

View File

@ -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)
}

View File

@ -0,0 +1,21 @@
<p>编写一个函数,检查输入的链表是否是回文的。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入: </strong>1-&gt;2
<strong>输出:</strong> false
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入: </strong>1-&gt;2-&gt;2-&gt;1
<strong>输出:</strong> true
</pre>
<p>&nbsp;</p>
<p><strong>进阶:</strong><br>
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?</p>
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 66</li><li>👎 0</li></div>