LeetCode——回文链表
题目
给定一个链表的头节点head,请判断该链表是否为回 文结构。
例如: 1->2->1,返回true。
1->2->2->1,返回true。
15->6->15,返回true。
1->2->3,返回false。
进阶:
如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。
代码实现:
//快慢指针,都是从头开始
public class IsPalindromeList {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
ListNode slow = head, fast = head;
ListNode pre = head, prepre = null;
while(fast != null && fast.next != null) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if(fast != null) {
slow = slow.next;
}
while(pre != null && slow != null) {
if(pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}
class ListNode {
int val;
// 下一个链表对象
ListNode next;
//赋值链表的值
ListNode(int x) { val = x; }
}
代码实现2:
//快慢指针,反转前半部分链表,时间O(n),空间O(1)
*,在移动指针时同时对前半部分链表进行反转。最后直接比较被分开的2个链表
*因为不能改变当前slow的next,不然就无法跳到下一个元素,所以这里用pre和prepre实现指针的反转
*时间复杂度:第一个循环O(n/2),第2个循环O(n/2)
public class IsPalindromeList {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return true;
ListNode slow = head, fast = head.next, pre = null, prepre = null;
while(fast != null && fast.next != null) {
//反转前半段链表
pre = slow;
slow = slow.next;
fast = fast.next.next;
//先移动指针再来反转
pre.next = prepre;
prepre = pre;
}
ListNode p2 = slow.next;
slow.next = pre;
ListNode p1 = fast == null? slow.next : slow;
while(p1 != null) {
if(p1.val != p2.val) {
return false;
}
p1 = p1.next;
p2 = p2.next;
}
return true;
}
}
class ListNode {
int val;
// 下一个链表对象
ListNode next;
//赋值链表的值
ListNode(int x) { val = x; }
}
LeetCode——回文链表的更多相关文章
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- Leetcode:234 回文链表
leetcode:234 回文链表 关键点:请判断一个链表是否为回文链表.示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true. ...
- Leetcode 234. 回文链表(进阶)
1.题目描述 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- leetcode面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
- 【leetcode 简单】 第六十七题 回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) 时间复 ...
- LeetCode OJ:Palindrome Linked List(回文链表判断)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- LeetCode 234——回文链表
1. 题目 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O( ...
随机推荐
- 提问(prompt)
prompt弹出消息对话框,通常用于询问一些需要与用户交互的信息.弹出消息对话框(包含一个确定按钮.取消按钮与一个文本输入框). 语法: prompt(str1, str2); 参数说明: str1: ...
- Qt 之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)
简述QBoxLayout可以在水平方向或垂直方向上排列控件,由QHBoxLayout.QVBoxLayout所继承. QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列. QVBox ...
- subprocess.call 使用
1.subprocess.call 里面的命令分开写,实例如下: subprocess.call 是不能作为赋值的,需要用到 subprocess.check_output 函数,而且如果要引用赋值就 ...
- Git(5):其他用法
分支操作 (1) 删除远程分支 $git remote add origin ssh://git@xxx.git ##如果未连接远程分支要先连接 $git push origin :<remot ...
- Linux 学习路径
Linux learning path Mind Map graph LR A[Linux学习路径]-->b[计算机概论与硬件相关知识] A -->c[Linux 初级] A --> ...
- 338.比特位计数( Counting Bits)leetcode
附上:题目地址:https://leetcode-cn.com/problems/counting-bits/submissions/ 1:题目: 给定一个非负整数 num.对于 0 ≤ i ≤ nu ...
- windows中service.msc与regedit
Services.msc是Windows2000/XP/2003/Vista/7/2008/8/8.1/10系统中用来启动.终止并设置 Windows 服务的管理策略. 作用:控制系统服务. 性质:系 ...
- POJ3734 Block母函数入门
一段长度为n的序列,你有红黄蓝绿四种颜色的砖块,一块砖长度为1,问你铺砖的方案数,其中红黄颜色之和必须为偶数. #include <queue> #include <stack> ...
- 关联SecureCRT
建议安装SecureCRT 8.1及其以上版本,打开http://EVE-NG地址/files/windows.zip下载eve-ng的Windows关联文件,然后使用文本编辑工具编辑win7_64b ...
- java按某属性分组并计算相关属性的和。
工作中在处理集合的时候会经常遇到需要分组然后计算某属性的和,在java8中,通过stream来操作集合,还是非常方便的,像过滤(filter).分组(group).获取单个属性的值,总而言之,简单方便 ...