LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1)。
分析:
(1)利用快慢指针找到链表的中心
(2)进行步骤(1)的过程中,对前半部分链表进行反转
(3)如果链表长是偶数,首先比较slow和slow->next的值是否相等,若不相等返回false,否则,比较以slow -> next -> next开头的链表和以suf1开头的链表比较是否相等
(4)如果链表长是奇数,则将以slow -> next开头的链表和以suf1开头的链表比较是否相等
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head == NULL) return true;
ListNode *fast = head;
ListNode *slow = head;
ListNode *suf1 = head -> next;
ListNode *suf2;
while(fast && fast -> next){
fast = fast -> next -> next;
suf2 = suf1 -> next;
suf1 -> next = slow;
slow = suf1;
suf1 = suf2;
}
head -> next = NULL;
if(fast){
slow = slow -> next;
}
else{
if(slow -> val != slow -> next -> val) return false;
slow = slow -> next -> next;
}
while(slow && suf1){
if(slow -> val != suf1 -> val) return false;
slow = slow -> next;
suf1 = suf1 -> next;
}
return true;
}
};
LeetCode 234. Palindrome Linked List(判断是否为回文链表)的更多相关文章
- [LeetCode]234. Palindrome Linked List判断回文链表
重点是: 1.快慢指针找到链表的中点.快指针一次走两步,慢指针一次走一步,分清奇偶数情况. 2.反转链表.pre代表已经反转好的,每次将当前节点指向pre /* 快慢指针得到链表中间,然后用206题方 ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 234. 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. 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 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- 234. Palindrome Linked List(判断链表是否回文)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
随机推荐
- 关于使用ssm与spring时,配置tomcat 虚拟目录( doBase )中的一些坑
一.使用SSM需要 配置虚拟目录时 tomcat的配置 在tomcat server.xml的<HOST></HOST>中加入以下内容 在配置完成之后,当我们访问URL 为 ...
- vue项目中解决跨域问题axios和
项目如果是用脚手架搭建的(vue cli)项目配置文件里有个proxyTable proxyTable是vue-cli搭建webpack脚手架中的一个微型代理服务器,配置如下 配置和安装axios 安 ...
- 联想ideapad关闭Fn
1.打开bios 开启/重启电脑的时候长按Fn+F2,就可以打开bios面板 2.切换到configuration菜单 使用键盘的右箭头将切换到configuration 3.关闭Fn 使用键盘下箭头 ...
- 记一次if控制器的使用
1.添加if控制器 2.输入判断条件:常见的就是某个变量是不是等于某个值 3.或者用函数助手中的函数 每个版本jmeter函数助手的入口不同,我的直接在菜单上: 选择__jexl3,输入判断条件,点击 ...
- IIS的部署(二)------虚拟目录的使用
IIS的虚拟目录 一个站点的网页的存储位置目录是固定的,而且结构和物理保存网页的磁盘路径相同.例如:默认网页的存储位置是c:\inetpub\wwwroot,当访问localhost即访问c:\ine ...
- JAVA中对null进行强制类型转换
今天很好奇,对null进行强转会不会抛错.做了如下测试得到的结果是,如果把null强转给对象,是不会抛异常的,本身对象是可以为null的.但是如果是基本类型,比如 int i = (Integer)o ...
- mybatis--第一个mybatis程序
首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...
- 【游戏体验】Colour My World(让我的世界充满色彩)
这是一款玩法简单的游戏 但是它给你的感觉不一样 推荐试玩 个人测评 游戏性 7/10 音乐 9/10 剧情 6/10 总评 22/30
- Angular 使用 frame 加载网络资源显示路径不安全问题
Angular 使用 frame 加载网络资源显示路径不安全问题 做项目的时候,angular 使用 frame 加载网络pdf文件的时候出现 unsafe value 问题,路径不安全.解决办法. ...
- IDF-CTF-简单的Elf逆向Writeup
ElfCrackMe1 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...