判断链表是否是回文。

我直接将链表的一半进行倒置,然后将两半的链表进行比较

 /**
* 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) {
int ln = , i = ;
ListNode* rhead = NULL;
ListNode* now, *rnow;
for(now = head; now; now = now->next, ++ln);
for(now = head; i < ln / ; ++i){
ListNode* tnow = now;
now = now->next;
tnow->next = rhead;
rhead = tnow;
}
if(ln % == ){
now = now->next;
}
for(rnow = rhead; now && rnow; rnow = rnow->next, now = now->next){
if(rnow->val != now->val) return false;
}
return true;
}
};

Leetcode 234 Palindrome Linked List 链表的更多相关文章

  1. [LeetCode] 234. Palindrome Linked List 回文链表

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

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

  3. LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

    翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...

  4. LeetCode 234. Palindrome Linked List(判断是否为回文链表)

    题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...

  5. [LeetCode]234. Palindrome Linked List判断回文链表

    重点是: 1.快慢指针找到链表的中点.快指针一次走两步,慢指针一次走一步,分清奇偶数情况. 2.反转链表.pre代表已经反转好的,每次将当前节点指向pre /* 快慢指针得到链表中间,然后用206题方 ...

  6. (easy)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 ...

  7. Java [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) ...

  8. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

  9. [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 ...

随机推荐

  1. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  2. WINDOWS的NTP配置

    将下面内容复制到记事本,保存成ntp.bat net stop w32Time REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeP ...

  3. Swift +AFNetworking3.0 Get

    let manager = AFHTTPSessionManager() let url = "http://v.juhe.cn/weather/index" let ," ...

  4. Thrift中实现按照时间戳范围操作Hbase数据

    在一次做项目的过程中,要实现一个功能,功能描述为前端给定日期范围,在该日期范围内取出指定行的信息.在Thrift常用的API中,取出一行所有的数据接口为getVer(),getver()具体描述如下: ...

  5. javascript学习第三课引用类型object

    主要内容: 1.object 是所有类型的基类 实例化对象: 1. var obj = new Object(); 2. var obj = {}; 设置对象属性和方法: obj.name = 'he ...

  6. Spring MVC 指导文档解读(一)

    22.1 指导文档章节 In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, whic ...

  7. Windows 8.1 应用再出发 - 创建我的第一个应用

    转眼间Windows 8.1已经发布了四个多月,之前因为开发需要对Windows 8.1新特性进行过零散的学习和使用,一直没有静下心来系统的学习过.近日部门有几名新同事加入,需要进行Windows 商 ...

  8. Android 开发必备

    Android 开发必备 http://www.androiddevtools.cn/ 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android ...

  9. 3.3 SQLite数据库

    1.使用嵌入式关系型SQLite数据库存储数据 轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用 ...

  10. 第43讲:Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析

    今天学习了scala的界定,先来看看下面这段代码 //class Pair[T] (val first : T,val second : T)class Pair[T <: Comparable ...