C++最简单的方法,遍历存在vector<int> ivec容器中,然后头尾对应比较

O(n)时间,O(n)空间

 /**
* 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* cur=head;
vector<int> ivec;
while(cur){
ivec.push_back(cur->val);
cur=cur->next;
}
for(int i=;i<ivec.size()--i;i++){
int j=ivec.size()--i;
if(ivec[i]!=ivec[j]) return false;
}
return true;
}
};

C++进阶方法:使用O(n)时间,O(1)空间,对前一半元素链表指向进行翻转,然后从中间到两边遍历判断,缺点是更改了原来的链表,好处是没有占用额外存储空间;

 /**
* 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* cur,*pre;
cur=head;
int length=;
while(cur!=NULL){
length+=;
cur=cur->next;
}
cur=head->next;pre=head;head->next=NULL;
for(int i=;i<(length+)/;i++){
ListNode* temp;
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
if(length%==) pre=pre->next;
while(cur!=NULL){
if(cur->val==pre->val){
cur=cur->next;pre=pre->next;
}
else return false;
}
return true;
}
};

leetcode 回文二叉树的更多相关文章

  1. leetcode回文子串拆分-最小拆分次数

    转载请注明来自souldak,微博:@evagle 上一篇是要输出所有的可能拆分,这回是要输出拆分次数最少的切割次数. 如果直接按照上一篇那么做的话,就会超时,因为我们在判断s[i][j]是否是回文的 ...

  2. Leetcode 回文数字判断

    一.问题描述 判断一个integer 型的数字是否是回文,空间复杂度应该是常数级别的 . 二.问题分析 首先,负数不是回文,10的整数倍不会是回文,个位数一定是回文. 三.代码实现 思路:将一个数字翻 ...

  3. leetcode 回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  4. Leetcode. 回文字符串的分割和最少分割数

    Q1: 回文字符串的分割 Given a string s, partition s such that every substring of the partition is a palindrom ...

  5. LeetCode——回文链表

    题目 给定一个链表的头节点head,请判断该链表是否为回 文结构. 例如: 1->2->1,返回true. 1->2->2->1,返回true. 15->6-> ...

  6. LeetCode 回文串问题

    5. Longest Palindromic Substring 647. Palindromic Substrings 解法一:从中心一点向两边扩展,需要考虑中心为一点,中心为两点. 解法二:马拉车 ...

  7. leetcode-分割回文子串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  8. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

随机推荐

  1. lnmp1.4 nginx配置thinkphp5

    vhost/xxx.conf配置图,重点是红色框框 研究了两三天 ,至此thinkphp5 路由,隐藏index.php全部解决 感谢网友的分享:http://blog.csdn.net/gaoxiu ...

  2. win7系统下MongoDB 4.0.1的安装

    环境: win7 - 64位系统 MongoDB下载地址: https://www.mongodb.com/download-center#community 版本: 4.0.1 安装步骤: 选择cu ...

  3. ubuntu部署Java、Python开发环境

    要部署Java开发环境首先就要安装JDK. 一.安装JDK8 1. 下载 jdk-8u172-linux-x64.tar.gz 到 /usr/java8/ 目录下: 2. tar  -zxvf  jd ...

  4. host - 使用域名服务器查询主机名字

    SYNOPSIS (总览) host [-l ] [-v ] [-w ] [-r ] [-d ] [-t querytype ] [-a ] host [server ] DESCRIPTION (描 ...

  5. 使用Python的pandas-datareader包下载雅虎财经股价数据

    0 准备工作 首先,使用pip方法安装pandas和pandas-datareader两个功能包. 安装的方法十分简单,以管理员身份运行cmd. 输入以下命令. $ pip install panda ...

  6. Linux--目录管理与文件管理--02

    ******Linux目录结构与目录管理******* 一.Linux目录结构: 1.目录创建规则:FHS文件系统层次化标准 指定了Linux操作系统的哪些目录是一定要具备的 2.目录的结构 树形结构 ...

  7. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  8. [易学易懂系列|rustlang语言|零基础|快速入门|(4)|借用Borrowing]

    [易学易懂系列|rustlang语言|零基础|快速入门|(4)] Borrowing 继续讲讲另一个重要的概念:借用(borrowing), 什么是借用? 我们先来看前一文章([易学易懂系列|rust ...

  9. python paramiko模块:远程连接服务器

    1.  SFTP基于 用户名密码 登录服务器,实现上传下载: import paramiko transport = paramiko.Transport(()) # 生成trasport,配置主机名 ...

  10. 一个web应用的诞生(3)

    经过上一章的内容,其实就页面层来说已结可以很轻松的实现功能了,但是很明显美观上还有很大的欠缺,现在有一些很好的前端css框架,如AmazeUI,腾讯的WeUI等等,这里推荐一个和flask集成很好的b ...