Leetcode_234_Palindrome Linked List
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334465
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
思路:
(1)题意为给定一个链表,判断该链表是否“回文”,即该链表中每个节点的值组成的串是否为回文串。
(2)下面对该题解法的时间复杂度为O(n),思路为:首先,申请两个StringBuffer用来保存数据;其次,遍历链表一次,将链表中每个节点的值加到StringBuffer中,在此过程中将链表逆序;最后,再次遍历逆序后的链表,将链表中每个节点的值追加到另一个StringBuffer中,然后比较两个StringBuffer是否完全相同,如果相同则回文,否则不是回文。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode;
import leetcode.utils.ListNode;
/**
* @author lqq
*/
public class Palindrome_LinkedList {
/* Could you do it in O(n) time and O(1) space? */
public static boolean isPalindrome(ListNode head) {
// 思路为将链表翻转,判断新旧链表是否对应位置元素相等
if (head == null || (head!=null&&head.next==null))
return true;
ListNode fis = head;
ListNode sed = head.next;
fis.next = null;
StringBuffer buffer = new StringBuffer();
buffer.append(fis.val);
while (sed != null) {
buffer.append(sed.val);
ListNode thd = sed.next;
sed.next = fis;
fis = sed;
sed = thd;
}
StringBuffer buffer2 = new StringBuffer();
while(fis!=null){
buffer2.append(fis.val);
fis = fis.next;
}
return buffer2.toString().equals(buffer.toString());
}
}
Leetcode_234_Palindrome Linked List的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- 高性能的Redis代理TwemProxy
TwemProxy是一个Redis的中间件代理,具有很多有用的功能,可以暂时替代一部分Redis Cluster的功能: ² 支持和6479.之后相应地,配置好两个Redis实例并启动.现在就可以启 ...
- [LaTex]插图
1.不错的Latex参考网站 http://www.ctex.org/documents/latex/graphics/node120.html http://www.ctex.org/documen ...
- 使用Fresco实现简单的显示一张图片
使用Fresco实现显示一张图片 仅仅是下载一张图片,在下载完之前,先显示一张站位图 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/de ...
- gitlab的搭建及问题的解决
gitlab则是类似于github的一个工具,github无法免费建立私有仓库,并且为了代码安全,于是在内网安装了一个自己实验室的一个git服务器,gitlab有很多依赖,而bitnami制作了一键安 ...
- 安卓自定义View实现图片上传进度显示(仿QQ)
首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling实现文档分类
http://blog.csdn.net/pipisorry/article/details/51525308 吉布斯采样的实现问题 本文主要说明如何通过吉布斯采样进行文档分类(聚类),当然更复杂的实 ...
- Ubuntu15.10 安装OpenCV3.1
wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip/downloa ...
- Uva - Uva272 - TEX Quotes
TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few ty ...
- Cocos2D:塔防游戏制作之旅(三)
整合炮塔资源 为了快速开始,我们为你创建了开始的项目.它包括了一个空白的Cocos2D项目以及大多数你将在教程中使用到的资源. 所以首先下载该 开始项目 并且解压缩到你指定的位置中去. 注意:该项目的 ...
- TCP的流量控制与拥塞控制小结
概述 为了提高信道的利用率TCP协议不使用停止等待协议,而是使用连续ARQ协议,意思就是可以连续发出若干个分组然后等待确认,而不是发送一个分组就停止并等待该分组的确认.其中TCP的流量控制与拥塞控制是 ...