234. Palindrome Linked List【easy】
234. Palindrome Linked List【easy】
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?
解法一:
/**
* 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 || head->next == NULL) {
return true;
} ListNode * slow = head;
ListNode * fast = head; while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next;
} slow->next = reverseList(slow->next);
slow = slow->next; while (slow != NULL) {
if (head->val != slow->val) {
return false;
}
slow = slow->next;
head = head->next;
} return true;
} ListNode * reverseList(ListNode* head)
{
if (head == NULL) {
return NULL;
} ListNode * pre = NULL;
while (head != NULL) {
ListNode * next = head->next;
head->next = pre;
pre = head;
head = next;
} return pre;
} };
关键点:
1、快慢指针找到中间位置
2、链表翻转

解法二:
class Solution {
public:
ListNode* temp;
bool isPalindrome(ListNode* head) {
temp = head;
return check(head);
}
bool check(ListNode* p) {
if (NULL == p) return true;
bool isPal = check(p->next) && (temp->val == p->val);
temp = temp->next;
return isPal;
}
};
递归,参考了@alex.tsitsura 的代码,就是利用了每一步递归的时候都有自己的栈空间的性质搞的。
一开始就是check(p->next)走到黑,然后就是到了最后一个节点了,我们比较一下最后一个节点和第一个节点的值;然后就执行下面的temp = temp->next,这个时候刚才那层递归也已经退栈了,现在我们的p对应的就是倒数第二个节点,temp对应的就是第二个节点;这样以此类推的搞下去,最后得解。
解法三:
上面的递归解法可能有些难以理解,可以改为如下:
class Solution {
public:
bool isPalindrome(ListNode* head) {
return check(head, head);
}
bool check(ListNode*& head, ListNode* p) {
if(!p) { return true; }
bool isPal = check(head, p->next);
if(head->val != p->val) {
return false;
}
head = head->next;
return isPal;
}
};
参考了@Meng-Ju 的代码
234. Palindrome Linked List【easy】的更多相关文章
- 234. Palindrome Linked List【Easy】【判断链表是否回文】
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 876. Middle of the Linked List【Easy】【单链表中点】
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
随机推荐
- 【贪心】Codeforces Round #402 (Div. 2) C. Dishonest Sellers
按照b[i]-a[i],对物品从大到小排序,如果这个值大于零,肯定要立刻购买,倘若小于0了,但是没买够K个的话,也得立刻购买. #include<cstdio> #include<a ...
- 显示/隐藏Mac系统中所有的隐藏文件
显示: 在终端输入:defaults write com.apple.finder AppleShowAllFiles YES 隐藏: 在终端输入:defaults write com.apple.f ...
- gns3 接口说明 转
Dynamips 支持的模块首先从C7200 开始Slot 0:C7200-IO-FE <------> 支持1 个Fastethernet 接口C7200-IO-2FE <---- ...
- Social regularizations
trust-aware :如何从隐式信任中导出显示信任.链接预测就是搞这一方面的么? 和类似谱聚类的拉普拉斯矩阵结合在一起,没怎么看.
- Inno Setup入门(十五)——Inno Setup类参考(1)
Inno setup脚本能够支持许多的类,这些类使得安装程序的功能得到很大的加强,通过对这些类的使用,将会创建出许多让人惊奇的安装程序,下面开始类的学习. 创建自定义向导页 自定义向导页需要在Init ...
- 简单php连接数据库作操作
1.近期稳定版本 <?php header('Content-Type: application/json'); $output = []; $host = ''; //MySQL服务器地址 $ ...
- MooseFS分布式文件系统介绍及安装教程详解
MFS系统由4个部分构成:master.metalogger.chunkserver.client. 1.Master —— mfs的大脑,记录着管理信息,比如:文件大小,存储的位置,份数等,和inn ...
- SpringMvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- scrapy-splash抓取动态数据例子十
一.介绍 本例子用scrapy-splash抓取活动行网站给定关键字抓取活动信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- Scala 将BigDecimal转换为Long
待转换.asInstanceOf[Number].longValue (Double转为Long也适用)