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 ...
随机推荐
- [BZOJ4817]树点涂色
第一个操作比较麻烦,但可以看出它和lct里的access操作差不多,所以可以利用lct的性质巧妙维护操作1 直接用lct维护树中同颜色的链(因为染色操作是从$x$染到根所以同颜色的点一定形成一条链), ...
- 【DFS】Gym - 101142C - CodeCoder vs TopForces
就按照题意建出有向图来(n个点,2n-2条边),然后从按随便一个rating排序,从最后一个开始dfs,用vis数组防止重复访问,因为每次之前的肯定能访问之后的(及之后的能访问的),所以不会有重复.就 ...
- 【分解质因数】【树状数组】【快速幂】codeforces 2014 ACM-ICPC Vietnam National Second Round E. ACM
乘除都在150以内,分解质因数后发现只有35个,建立35个树状数组/线段树,做区间加.区间查询,最后快速幂起来. #include<cstdio> #include<cstring& ...
- redis踩坑记
本来打算给一批主库做从库,用来读取数据,还不想碰主库数据. 主库redis2.8.12,从库一开始没注意,docker了一个3.1的,结果slaveof之后命令不兼容,下了一个2.8的(2.8.23好 ...
- golang设计模式-成员变量赋值
常见golang的struct赋值有两种: 1)定义变量同时初始化 val := &Options{ UID:int(1), } 2)先定义变量,再赋值 val := new(Options) ...
- Swift中结合使用枚举与Switch
定义: 用法:
- dwz中弹出的窗口页面如何获取前页面(点击按钮的页面)的元素???
在页面A.jsp中点击一个按钮,使用$.pdialog.open()方法弹出b.jsp页面(对话框窗口),我要在b.jsp中选中值然后关闭窗口(b.jsp)返回值给A.jsp~ =========== ...
- Git学习笔记(一) 安装及版本库介绍
安装Git 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和 ...
- Android 多线程之IntentService 完全详解
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
- sersync部署
rsync : wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz Sersync: wget https://raw.git ...