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. POJ2002 &&HDU5365 判断给定的点中有多少个不同的正方形

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17740   Accepted: 6776 Descript ...

  2. 深入X64架构(翻译)

    | 本人只是原创翻译,而且翻译也不一定好,纯当锻炼.内容如果英文好的同学,建议直接去看英文原版,比较爽. NBAOL系列2代产品是 windows平台64位的应用程序,在技术测试过程中,遇到一些cra ...

  3. 代码神器:拒绝重复编码,这款IDEA插件了解一下.....

    作者:HeloWxl www.jianshu.com/p/e4192d7c6844 Easycode是idea的一个插件,可以直接对数据的表生成entity.controller.service.da ...

  4. LeetCode做题笔记之动态规划

    LeetCode之动态规划 时间有限只做了下面这几道:70.338.877.96.120.95.647,后续会继续更新 70:爬楼梯 先来道简单的练练手,一道经典的动态规划题目 可以采用动态规划的备忘 ...

  5. redis常用命令--基础

    redis默认有个数据库,下标从开始,不支持自定义数据库名字. 使用select  index 切换数据库  : select 7 (切换到第八个数据库) dbsize:求当前数据库中键值对的数量. ...

  6. 安装adobe reader阅读器

    首先  在我的网盘里有那个软件. 安装的教程在这个歌网址:http://www.zhanshaoyi.com/6730.html

  7. PAT A1005-1008

    A 1005 Spell It Right (20 point(s)) 25分的题目,比较简单,注意N的范围,用字符串处理即可. #include <iostream> #include ...

  8. reference-based measure|Distribution-based measure|密码子使用偏向性

    生命组学 密码子使用偏向性是指同义密码子使用频率不同. 影响因素:1.GC2.横向基因转移3.selection 转录偏好于多的tRNA. 同种氨基酸但有密码子使用偏向. ============== ...

  9. MySQL--主备相关命令

    创建用户账号 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'10.70.8.%' IDENTIFIED BY 'mysql'; ...

  10. [NOI2017]蚯蚓排队(链表+hash)

    这题看题面感觉挺玄学的,但其实会挂链式hash就能暴力切了,就是纸老虎,考察选手的语文水平.不过三年没写挂链hash也应该写一下了…… 首先模数设成自然溢出ull,然后挂链时的模数取2^24.然后就可 ...