Palindrome Linked List leetcode
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?
Subscribe to see which companies asked this question
bool isPalindrome(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return true;
// 寻找中间结点
ListNode *slow = head;
ListNode *fast = head;
while (fast->next != nullptr && fast->next->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
}
// 逆置后部分链表
ListNode *newList = slow->next;
ListNode *curNode = slow->next->next;
newList->next = nullptr;
while (curNode != nullptr)
{
ListNode *temp = curNode->next;
curNode->next = newList;
newList = curNode;
curNode = temp;
}
// 依次比较
while (newList != nullptr)
{
if (newList->val != head->val)
return false;
newList = newList->next;
head = head->next;
}
return true;
}
Palindrome Linked List leetcode的更多相关文章
- 234. Palindrome Linked List - LeetCode
Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- LeetCode_234. Palindrome Linked List
234. Palindrome Linked List Easy Given a singly linked list, determine if it is a palindrome. Exampl ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- [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. Palindrome Linked List (回文链表)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
随机推荐
- Cracking the code interview
推荐一本书<Cracking the code interview> Now in the 5th edition, Cracking the Coding Interview gives ...
- 如何编译POCO
Poco C++库是: 一系列C++类库,类似Java类库,.Net框架,Apple的Cocoa; 侧重于互联网时代的网络应用程序 使用高效的,现代的标准ANSI/ISO C++,并基于STL 高可移 ...
- 负载均衡软件LVS分析三(配置)
LVS集群有DR.TUN.NAT三种配置模式,可以对www服务.FTP服务.MAIL服务等做负载均衡,下面通过搭建www服务的负载均衡实例,讲述基于DR模式的LVS集群配置. 一. Director ...
- SQL Server 2014内存优化表的使用场景
SQL Server 2014内存优化表的使用场景 最近一个朋友找到走起君,咨询走起君内存优化表如何做高可用的问题 大家知道,内存优化表是从SQL Server 2014开始引入,可能大家对内存优化表 ...
- GIT团队开发操作
01. 建立代码仓库(专门用于团队开发的代码仓库) ========================================================================== ...
- 蓝桥网试题 java 基础练习 十六进制转十进制
---------------------------------------------------------------------------------------- 貌似用int类型不会超 ...
- 《响应式Web设计—HTML5和CSS3实战》 学习记录
作者:Ben Frain 学习时间 2016/5/12 第一章 设计入门 *视口调试工具 IE:Microsoft Internet Explorer Develop Toolbar Safa ...
- 应用 EditPlus 配置 Java 编译环境
此文全文摘抄自: http://jingyan.baidu.com/album/37bce2be3ceef61002f3a208.html?picindex=7,谢啦 应用成功: EditPlus(文 ...
- java字符串比较及小数浮点型的使用
import java.text.DecimalFormat; /* * 小数类型的常量默认是double类型,声明float类型的常量需要使用F作为后缀. * * 关于equals()和==: 对于 ...
- python 模块加载错误总结
在运行yum时提示如下错误 There was a problem importing one of the Python modules required to run yum. The error ...