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. 将java项目转成svn项目

    1.需要工具:eclipse / svn插件 2.在需要共享的项目上右键-->team-->Share Project分享项目 3.选择svn 如果需要共享成cvs选择cvs即可. 4.选 ...

  2. 用Spring中的ResponseEntity文件批量压缩下载

    我看了很多网上的demo,先生成ZIP压缩文件,然后再下载. 我这里是生成ZIP文件流 进行下载.(核心代码没多少,就是一些业务代码) @RequestMapping(value = "/& ...

  3. hash表系列(转)

    http://www.cnblogs.com/mumuxinfei/p/4441826.html 前言: 我以前在百度的mentor, 在面试时特喜欢考察哈希表. 那时的我满是疑惑和不解, 觉得这东西 ...

  4. spring 官方文档-片段学习——webflux-ann-controller

    spring 官方文档-片段学习总结 片段所在连接:https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-referenc ...

  5. sqlserver 存储过程 C#调用 实现从数据库Get数据

    在最近的项目中我想建立一个EFDBfirst的模型但是失败了,生成的edmx中没有实体类和表结构,到处需求解决方案,未果. 问题请见:https://q.cnblogs.com/q/102743/ 后 ...

  6. Java8集合框架——LinkedList源码分析

    java.util.LinkedList 本文的主要目录结构: 一.LinkedList的特点及与ArrayList的比较 二.LinkedList的内部实现 三.LinkedList添加元素 四.L ...

  7. 大数据攻城狮之进阶技能-使用Git上传自己的项目至GitHub仓库

    在开始讲解之前我们默认已经存在仓库和安装Git,没有下载的可以去https://git-scm.com/ Git官方网站下载或者是私聊博主. 下面我们说一说Git的使用,比如说如何生成密钥,连接远程仓 ...

  8. linux 替换jdk指定jar包

    我的bug是:jdk1.8的安全策略和腾讯邮箱服务有冲突.我知道的解决方法: 1更换低版本安全策略相关的jar包.(windows:http://www.cnblogs.com/dennyzhangd ...

  9. javascript函数柯里化初探

    // 柯里化之前 function add(x,y,z){ return x+y+z; } add(1,2,3) // 6 // 柯里化之后 function curryAdd(x){ return ...

  10. 吴裕雄--天生自然MySQL学习笔记:MySQL 查询数据

    MySQL 数据库使用SQL SELECT语句来查询数据. 可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MySQL数据库中查询数据通用 ...