Given a linked list, check whether it is a palindrome.

Examples:

Input:   1 -> 2 -> 3 -> 2 -> 1 -> null

output: true.

Input:   1 -> 2 -> 3 -> null

output: false.

/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
// Write your solution here
ListNode revNode = reverse(head);
ListNode cur = head;
while (cur != null && revNode != null) {
if (cur.value != revNode.value) {
return false;
}
cur = cur.next;
revNode = revNode.next;
}
return cur == null && revNode == null;
} private ListNode reverse(ListNode node) {
ListNode head = null;
while (node != null) {
ListNode newNode = new ListNode(node.value);
newNode.next = head;
head = newNode;
node = node.next;
}
return head;
}
}
/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
// Write your solution here
if (head == null || head.next == null) {
return true;
}
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode mid = slow;
mid.next = reverse(mid.next); ListNode revNode = mid.next;
ListNode cur = head;
while (cur != null && revNode != null) {
if (cur.value != revNode.value) {
return false;
}
cur = cur.next;
revNode = revNode.next;
}
return true;
} private ListNode reverse(ListNode node) {
ListNode prev = null;
while (node != null) {
ListNode nxt = node.next;
node.next = prev;
prev = node;
node = nxt;
}
return prev;
}
}

[Algo] 306. Check If Linked List Is Palindrome的更多相关文章

  1. Data Structure Linked List: Function to check if a singly linked list is palindrome

    http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ 这里的reverse可以re ...

  2. [Algo] 131. Deep Copy Linked List With Random Pointer

    Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...

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

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

  4. [Linked List]Remove Nth Node From End of List

    Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...

  5. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  6. Palindrome Partitioning I & II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  7. Chp4: Trees and Graphs

    1.Type of Tree 1. Binary Tree: a binary tree is a tree in which each node has at most two child node ...

  8. 《Cracking the Coding Interview》——第2章:链表——题目7

    2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...

  9. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

随机推荐

  1. Arduino - ( Uno、Nano、Promini)针脚示意图

    Uno针脚示意图 Nano针脚示意图 Promini针脚示意图

  2. spark on yarn 安装笔记

    yarn版本:hadoop2.7.0 spark版本:spark1.4.0 0.前期环境准备: jdk 1.8.0_45 hadoop2.7.0 Apache Maven 3.3.3 1.编译spar ...

  3. BZOJ 5059 前鬼后鬼的守护

    题解: 解法一:用函数斜率什么的,不会,留坑 解法二: 某一个序列都变成一个值那么中位数最优 加入一个元素,与前面那一段区间的中位数比较 x>=mid什么事也不做 x<mid合并两端区间 ...

  4. JVM探秘:jinfo查看JVM运行时参数

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 如何查看JVM运行时参数,对于线上JVM调优是很关键的,因为只有知道了当前使用的JVM ...

  5. Android自定义View——贝塞尔曲线实现水波纹效果

    我们使用到的是Path类的quadTo(x1, y1, x2, y2)方法,属于二阶贝塞尔曲线,使用一张图来展示二阶贝塞尔曲线,这里的(x1,y1)是控制点,(x2,y2)是终止点,起始点默认是Pat ...

  6. CF1217A Creating a Character

    You play your favourite game yet another time. You chose the character you didn't play before. It ha ...

  7. @SpringBootApplication 标注非引导类

    1.引导类 public class App { public static void main(String[] args) { SpringApplication.run(WebConfigura ...

  8. Cordova搭建环境与问题小结

    1.Cordova介绍: Apache Cordova是一套设备API,允许移动应用的开发者使用JavaScript来访问本地设备的功能,比如摄像头.加速计.它可以与UI框架(如jQuery Mobi ...

  9. C#调用C++系列二:传结构体

    这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...

  10. Codeforces 400C 矩阵乘法 数学规律

    今天下午Virtual了一套最近的CF题,第三题给TLE了,就跑过去上课了. 这题给定一个由二进制表示的矩阵,当询问3的时候,求矩阵的值,矩阵的值是所有第i行乘以第i列的值的总和,然后还有1 b是翻转 ...