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. 基于HttpRunner,解析swagger数据,快速生成接口测试框架

    使用HttpRunner默认生成的项目是这样的 命令:httprunner --startproject  项目名称 so,根据这个项目的目录结构,使用python解析swagger接口参数,可以快速 ...

  2. yii报错yii\web\Request::cookieValidationKey must be configured with a secret key.

    在config文件下main-local.php配置 'cookieValidationKey' => 'rabbit1234',

  3. css sticky footer 布局

    方法一:footer 上用负的 margin-top 在内容外面需要额外包一层元素(wrap)来让它产生对应的 padding-bottom.是为了防止负 margin 导致 footer 覆盖任何实 ...

  4. 总结下Nginx的功能模块

    nginx-1.10.3]# ./configure  \ --prefix=/usr/local/nginx   \        #指定安装路径 --user=nginx --group=ngin ...

  5. ifconfig命令返回找不到“-bash: ifconfig: command not found”

    “-bash: ifconfig: command not found“因为系统没有安装net-tools yum -y install net-tools

  6. mysql5.7.26做主从复制配置

    一.首先两台服务器安装好mysql数据库环境 参照linux rpm方式安装mysql5.1 https://www.cnblogs.com/sky-cheng/p/10564604.html 二.主 ...

  7. php关于jquery ajax json不返回数据的问题

    分析原因: 1.php端代码有错导致echo输出错误,导致ajax返回函数不执行 2.jquery版本原因 3.juqery前端script代码问题

  8. DirectX屏幕捕获和输出视频

    #include <Windows.h> #include <mfapi.h> #include <mfidl.h> #include <Mfreadwrit ...

  9. 移动端布局基础viewport

    划重点 手机屏幕相对着桌面浏览器小,传统网页的设计在手机上体验糟糕 Apple 在移动版 Safari 中定义了 viewport meta 标签(如果没记错最早提出的话),用于创建一个虚拟窗口(la ...

  10. python类库26[sqlite]

    一 sqlite 与 python 的类型对应 二 实例 import sqlite3 def sqlite_basic():     # Connect to db     conn = sqlit ...