LeetCode234_PalindromeLinkedList (推断是否为回文链表) Java题解
题目:
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
题解:
推断一个链表是不是回文的,这里要求O(n)时间复杂度和O(1)的空间时间复杂度。总共想了三种办法,三种办法都用到了两个指针,符合题目要求的仅仅有最后一种。
第一种办法:用数组倒着存前半段的链表的值。然后和后半段链表的值进行比較。
这样的解法执行的时间最久可能是由于数组倒着插入比較耗时。
代码:
//用数组实现 o(n/2)空间
public static boolean isPalindrome(ListNode head) { // ArrayList<Integer> nodeVal=new ArrayList<>();
LinkedList<Integer> nodeVal=new LinkedList<>(); if(head==null||head.next==null)
return true;
ListNode slow=head;
ListNode fast=head; nodeVal.add(0,slow.val);
while(fast.next!=null&&fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
nodeVal.add(0,slow.val);
} ListNode cur=slow;
if(fast.next!=null)//链表长度为偶数
cur=slow.next;
int i=0;
while(cur!=null)
{
if(nodeVal.get(i)!=cur.val)
return false;
cur=cur.next;
i++;
}
return true;
}
另外一种解法:在第一种的思路的基础上。我们要实现一个倒序,我们干嘛不用现成的数据结构-栈。于是把链表前半段压栈,然后出栈和后面的链表依次比較,这样的执行时间最短。但由于用到了栈还是不符合题目要求。
代码:
//用栈实现
public static boolean isPalindrome2(ListNode head) { Stack<ListNode> stack=new Stack<>();
ListNode slow=head;
ListNode fast=head; if(fast==null||fast.next==null)//0个节点或是1个节点
return true; stack.push(slow);
while(fast.next!=null&&fast.next.next!=null)
{ fast=fast.next.next;
slow=slow.next;
stack.push(slow);
}
if(fast.next!=null)//链表长度为偶数
slow=slow.next; ListNode cur=slow;
while(cur!=null)
{
if(cur.val!=stack.pop().val)
return false;
cur=cur.next;
}
return true; }
第三种:我们这样想,我们可不能够不借助外在的存储实现倒序呢,事实上是能够的,链表反转的时候我们就没有借助外在存储。
思路是把后半段的原地链表反转然后和前半段进行比較(当然你也能够反转前半段)执行时间略微比另外一种慢一些。可是符合题目O(1)空间复杂度的要求
代码:
//链表原地转置实现o(1)空间复杂度
public static boolean isPalindrome3(ListNode head) {
ListNode slow=head;
ListNode fast=head; if(fast==null||fast.next==null)//0个节点或是1个节点
return true; while(fast.next!=null&&fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
}
//对链表后半段进行反转
ListNode midNode=slow;
ListNode firNode=slow.next;//后半段链表的第一个节点
ListNode cur=firNode.next;//插入节点从第一个节点后面一个開始
firNode.next=null;//第一个节点最后会变最后一个节点
while(cur!=null)
{
ListNode nextNode=cur.next;//保存下次遍历的节点
cur.next=midNode.next;
midNode.next=cur;
cur=nextNode;
} //反转之后对前后半段进行比較
slow=head;
fast=midNode.next;
while(fast!=null)
{
if(fast.val!=slow.val)
return false;
slow=slow.next;
fast=fast.next;
}
return true; }
LeetCode234_PalindromeLinkedList (推断是否为回文链表) Java题解的更多相关文章
- 领扣(LeetCode)回文链表 个人题解
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...
- Java判断链表是否为回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 思路:1.通过快慢指针,来遍历链表 ...
- Java实现 LeetCode 234 回文链表
234. 回文链表 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否 ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- 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 ...
- 234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...
- [LC]234题 Linked List Cycle (回文链表)(链表)
①中文题目 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2输出: false示例 2: 输入: 1->2->2->1输出: true进阶:你能否用 O(n) 时间 ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- leetcode面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
随机推荐
- 数据库恢复挂起解决办法【MSSQL】
新建查询输入如下代码运行 - -把test改成你需要修复的数据库名 USE master GO ALTER DATABASE test SET SINGLE_USER GO ALTER DATABAS ...
- 对socket的理解
要想理解socket,就得先熟悉TCP/IP协议族,TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,定义了主机如 ...
- React 篇 Comment Model
Model 原型 Comment Box <div className="commentBox"> <h1>Comments</h1> < ...
- Flask Web 发送邮件单文件
import os from flask import Flask, render_template, session, redirect, url_for from flask_script imp ...
- postgreSQL在Centos6下编译安装
1.准备安装源 下载地址:https://www.postgresql.org/ftp/source/ 下载并解压. 2.软件编译安装 配置.检查安装环境 ./configure --prefix=/ ...
- 搭建linux环境:如何在vmware安装linux虚拟机??
本来不想再整一遍的,奈何分布式压测呀,呀呀呀呀呀呀 1.安装linux虚机 (1)在桌面上双击VMware Workstation图标后启动虚拟机,鼠标单击文件,选择新的虚拟机: (2)单击“next ...
- day19-常用模块IV(re、typing)
目录 re模块 typing模块 爬取音频 re模块 用来从字符串(文本)中查找特定的东西 1.元字符:有特殊意义的字符 ^ 从开头匹配 import re a = re.findall('^abc' ...
- 【原】Mysql常用语句
1.修改编码方式为UTF-8 ALTER TABLE 表名 CHANGE 列名 新列名 VARCHAR(255) CHARACTER SET utf8 COLLATE ...
- java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...
- Makefile,Shell command,Shell Language 之间的联系
1. Makefile 首先要知道Makefile 是什么东西,Makefile 是一个指令文件,里面存储着自定义的命令(可以借助已有的命令创造而来)在不同的系统下对Makefile 的区别不一样,L ...