Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Follow up:
Could you do it in O(n) time and O(1) space?

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return true;
//根据快慢指针求中点
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
if(fast != null)
slow = slow.next;
//slow是中点,将中点反转后
slow = reverse(slow);
fast = head;
while(slow != null) {
if(slow.val != fast.val)
return false;
slow = slow.next;
fast = fast.next; }
return true;
}
public ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode next = null;
while(head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}

234. Palindrome Linked List【Easy】【判断链表是否回文】的更多相关文章

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

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

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

    原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

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

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

  6. 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. 【easy】234. Palindrome Linked List

    ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...

  8. 【leetcode】234. Palindrome Linked List

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

  9. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

随机推荐

  1. HNOI 2016 地图

    [题目描述] Hoshizora Rin是个特别好动的少女. 一天Rin来到了一个遥远的都市.这个都市有N个建筑,编号从1到N,其中市中心编号为1,这个都市有M条双向通行的街道,每条街道连接着两个建筑 ...

  2. bzoj 2038 莫队入门

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...

  3. 2015/8/29 Python基础(3):数值

    数字提供了标量储存和直接访问,是不可更改类型,每次变更数值会产生新的对象.Python支持多种数字类型,包括整型.长整型.布尔型.双精度浮点.十进制浮点和复数.在Python中,变量并不是一个盒子,而 ...

  4. 「模板」 FHQ_Treap

    「模板」 FHQ_Treap 我也是偶然发现我还没发过FHQ_Treap的板子. 那就发一波吧. 这个速度实在不算快,但是不用旋转,并且好写. 更重要的是,Splay 可以做的事情它都可以做!比如区间 ...

  5. PHP把数据库数据导入Excel

    <?php function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; ...

  6. mixin模式特点

    mixin模式特点: 1.单一功能, 2.不和基类关联,可以和任意基类组合,基类可以不和mixin关联就可以初始化成功 3.不使用 super() 用法

  7. Freemarker的配置与使用

    1.在pro.xml配置文件中引入架包 <!--freemarker-->  <dependency>   <groupId>freemarker</grou ...

  8. 【Codeforces549F】Yura and Developers [单调栈][二分]

    Yura and Developers Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 ...

  9. 【BZOJ4868】期末考试 [三分][贪心]

    期末考试 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Samp ...

  10. uoj308 【UNR #2】UOJ拯救计划

    传送门:http://uoj.ac/problem/308 [题解] 考虑枚举用了$i$所学校,那么贡献为${k \choose i} * cnt * i!$ 意思是从$k$所选$i$所出来染色,$c ...