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 ...
随机推荐
- 1.5(Spring MVC学习笔记) 拦截器(Interceptor)
一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...
- Linux虚拟机小问题解决方法系列
1)使用虚拟机的过程中,会碰到虚拟机占用的空间越来越大的情况,即使删除了虚拟机里的文件,磁盘空间还是似乎还是没有释放,使用“vmware-vdiskmanager”工具解决.解决方法在这里:参考.合并 ...
- 动态RIP配置路由表
动态RIP配置路由表 以Router11为例子: (1)配置端口ip(两个端口需要设置两个ip) Router(config)#inter f0/0 Router(config-if)#ip add ...
- OC语言基础之NSArray
0.数组的分类 NSArray :不可变数组 NSMutableArray : 可变数组 1: // @[] 只创建不可变数组NSArray 2: /* 错误写法 3: NSMutableArray ...
- Why DNS Based Global Server Load Balancing (GSLB) Doesn’t Work
Why DNS Based Global Server Load Balancing (GSLB) Doesn't Work
- 解决ThinkPHP3.2.3框架,PDO驱动查询出来的字段名全是小写的bug
找到文件:ThinkPHP\Library\Think\Db\Driver.class.php 找到代码: // PDO连接参数 protected $options = array( PDO::AT ...
- Cisco模拟器Web-IOU使用说明 转
http://blog.sina.com.cn/s/blog_af0abf1f0102uztk.html GNS3作为使用最多的Cisco官方模拟器,是因为它使用简单,所有设置图形化,是一款非常强 ...
- IDEA中添加类的创建者信息
创建方法: 1. 使用快捷键(ctrl + alt + s),在弹出框中左边侧选择 Editor -> File and Code Templates,左边侧相应会更新 右边侧选择 Class, ...
- Bootstrap响应式折叠导航
浏览器宽度缩小到一定值时,导航显示为 <nav class="navbar navbar-inverse navbar-fixed-top" role="navig ...
- kettle新手教程
1.kettle介绍 kettle是一个ETL(Extract, Transform and Load抽取.转换.加载)工具,ETL工具在数据仓库项目使用很频繁,kettle也能够应用在下面一些 ...