[Algo] 306. Check If Linked List Is Palindrome
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的更多相关文章
- 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 ...
- [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 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- 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 ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 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 ...
- 《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...
- 10个经典的C语言面试基础算法及代码
10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...
随机推荐
- JVM源码分析之自定义类加载器如何拉长YGC
概述 本文重点讲述毕玄大师在其公众号上发的一个GC问题一个jstack/jmap等不能用的case,对于毕大师那篇文章,题目上没有提到GC的那个问题,不过进入到文章里可以看到,既然文章提到了jstac ...
- JPA 开发中遇到的错误
JPA 开发中遇到的错误 (2011-07-13 16:56:12) 转载▼ 标签: 杂谈 分类: Java/J2EE 常见异常1.异常信息:org.hibernate.hql.ast.QuerySy ...
- 实验吧Web-易-天网管理系统(php弱类型,==号)
打开网页,查看源码,看到 <!-- $test=$_GET['username']; $test=md5($test); if($test=='0') --> 说明用户名需要加密之后为0. ...
- 学习spring的第一天
1.首先在maven repository中找到Spring Context依赖添加进模块 2.配置xml,resources右键new→xml configuration file→Spring C ...
- ansible shell 之运行后台程序
最近在使用ansible shell模块启动一个shell编写的脚本,该脚本主要功能式加载java的classpath并在后台运行这个java程序. 该脚本在linux shell中可以正常启动和停止 ...
- 在h5页面上添加音乐播放
接到需求说要做一个h5轮播图,同时配上背景音乐. Html部分: <!--音乐开始--> <div id="music"> <div id=" ...
- POJ 1836:Alignment
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14492 Accepted: 4698 Descri ...
- ES7之async/await
async 是 ES7 才有的与异步操作有关的关键字. async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数. async function helloAsync(){ ...
- Linux下yum出现no module named pycurl 解决办法
1.1 no module named pycurl 解决办法 下载curl:http://curl.haxx.se/download/curl-7.21.3.tar.gz .tar.gz ./con ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...