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】的更多相关文章

  1. 234. Palindrome Linked List【Easy】【判断链表是否回文】

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  2. 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 ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  8. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  9. 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. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  2. 【数论】【枚举约数】【友好数】CODEVS 2632 非常好友

    O(sqrt(n))枚举约数,根据定义暴力判断友好数. #include<cstdio> #include<cmath> using namespace std; int n; ...

  3. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  4. 【进制转换】codevs 1474 十进制转m进制

    #include<cstdio> using namespace std; ],en; int main() { scanf("%d%d",&n,&m) ...

  5. Problem W: 零起点学算法21——求平均值

    #include<stdio.h> int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); pr ...

  6. 用 HTML 元素实现自定义的滚动条

    有时,浏览器默认的滚动条不能满足需求,我们要实现自定义的滚动条.借助于鼠标移动事件和滚轮事件,以及内容元素的滚动相关属性,可以很容易地实现这样的需求.下面就来试一试. 我们这次要实现的滚动条需要有以下 ...

  7. jvm-监控指令-jstack

    格式: jstack [option] vmid 选项: -l 除了堆栈信息外,显示关于锁的附加信息. 作用: 生成虚拟机当前时刻的线程快照. 目的: 定位线程长时间停顿的原因,比如线程间死锁.死循环 ...

  8. ASP.NET Core 1.0基础之静态文件处理

    来源 这些HTML , CSS files, image files, 和JavaScript这些静态文件,是ASP.NET能够直接响应给客户端的.本文详述下ASP.NET和静态文件的关系. Serv ...

  9. Dedecms getip()的漏洞利用

    flyh4t在非安全发布了dedecms getip()的注射漏洞,漏洞本身的成因没什么好说的老掉牙的X-Forwarded-For的问题,我想这个漏洞很多人都找到了,不过这个漏洞的利用有个地方还是可 ...

  10. Keen Team

    Keen Team (碁震安全研究团队,KeenTeam)是一支由在信息安全理论和技术研究方面全球领先的中国“白帽”安全专家组成的信息安全研究队伍,成员主要来自微软的安全漏洞研究.安全攻击和防御技术研 ...