LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1)。
分析:
(1)利用快慢指针找到链表的中心
(2)进行步骤(1)的过程中,对前半部分链表进行反转
(3)如果链表长是偶数,首先比较slow和slow->next的值是否相等,若不相等返回false,否则,比较以slow -> next -> next开头的链表和以suf1开头的链表比较是否相等
(4)如果链表长是奇数,则将以slow -> next开头的链表和以suf1开头的链表比较是否相等
/**
* 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) return true;
ListNode *fast = head;
ListNode *slow = head;
ListNode *suf1 = head -> next;
ListNode *suf2;
while(fast && fast -> next){
fast = fast -> next -> next;
suf2 = suf1 -> next;
suf1 -> next = slow;
slow = suf1;
suf1 = suf2;
}
head -> next = NULL;
if(fast){
slow = slow -> next;
}
else{
if(slow -> val != slow -> next -> val) return false;
slow = slow -> next -> next;
}
while(slow && suf1){
if(slow -> val != suf1 -> val) return false;
slow = slow -> next;
suf1 = suf1 -> next;
}
return true;
}
};
LeetCode 234. Palindrome Linked List(判断是否为回文链表)的更多相关文章
- [LeetCode]234. Palindrome Linked List判断回文链表
重点是: 1.快慢指针找到链表的中点.快指针一次走两步,慢指针一次走一步,分清奇偶数情况. 2.反转链表.pre代表已经反转好的,每次将当前节点指向pre /* 快慢指针得到链表中间,然后用206题方 ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 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 ...
- [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 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- 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 ...
随机推荐
- (转)多进程 & 多线程的区别与适用场景
转自:http://www.cnblogs.com/huntfor/p/4021327.html 关于多进程和多线程,教科书上最经典的一句话是“进程是资源分配的最小单位,线程是CPU调度的最小单位”, ...
- 搭建FEBS权限系统
在码云看到一个FEBS权限系统,但是没有找到搭建手册,自己记录一下. 1.下载项目:https://github.com/wuyouzhuguli/FEBS-Shiro2.创建数据库:执行sql文件夹 ...
- poj 3057(bfs+二分匹配)
题目链接:http://poj.org/problem?id=3057 题目大概意思是有一块区域组成的房间,房间的边缘有门和墙壁,'X'代表墙壁,'D'代表门,房间内部的' . '代表空区域,每个空区 ...
- jmeter的使用---控制器
1.如果(If)控制器.Switch Controller if控制语句,判断字段是否存在,或者符合,执行不同的逻辑 2.简单控制器 一次进件流程,需要不同模块的数据,例如登陆,提交个人信息,信用认证 ...
- Java - JVM - 类的生命周期
概述 简述 JVM 里 类的生命周期 上次写了 30%, 居然丢了 难受, 又要重新写 类的生命周期 加载 使用 卸载 1. 加载 概述 类型的加载 大体流程 装载 连接 验证 准备 解析(可选的) ...
- blog主题——马路
blog主题,存储一下 css /************************************************** 第一部分:所有的模板都使用的公共样式.公告样式是为了更好的向前 ...
- Getopt::Long - Extended processing of command line options
use Getopt::Long; my $data = "file.dat"; my $length = 24; my $verbose; GetOptions (" ...
- 那些年做过的ctf之加密篇(加强版)
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- 关于C++指针、引用和const关键字的各种关系
#include <stdio.h> #include<iostream> using namespace std; typedef char *new_type; int m ...
- [HTTP]HTTP/1.1 协议Expect: 100-continue
在追踪请求时发现了这么一个http头 基础知识背景:1)“Expect: 100-continue”的来龙去脉: HTTP/1.1 协议里设计 100 (Continue) HTTP 状态码的的目的是 ...