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 ...
随机推荐
- 没事不要在for循环期间增减迭代序列的成员
>>> arr=[4, 4, 9, 7, 7] >>> for i,a in enumerate(arr): arr.pop(i) print(i,a) 4 0 4 ...
- SQL Server专家的10个秘诀(翻译加注解)
当你点开这篇文章的时候,如果觉得没有读下去的必要,也希望你能拉到最后看看那几行字! 原文出处:https://technet.microsoft.com/en-us/magazine/gg299551 ...
- Android Studio安装Genymotion插件
Android Studio安装Genymotion插件 Eclipse就不介绍了,谷歌都已经放弃Eclipse了,你还在坚持什么. 安装Genymotion 官网:https://www.genym ...
- MTK8127源码编译出现的错误及相关解决办法
/** * date:2016/8/17 * author: Y.X .YANG */ 按照开发文档提示: 1.MTK提供的开发包目录下有若干个.aa .ab .ac ...的分压缩包.此时应当将这些 ...
- 2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口
1 新建一个空项目 A 编写 .pro文件 QT += gui widgets HEADERS += \ MyDialog.h SOURCES += \ MyDialog.cpp B 编写MyD ...
- Android数据库Sqlite-android学习之旅(九)
简介 sqilte是一个轻量级的数据库,满足数据库的基本操作,由于移动端的内存有限,所以sqilte刚好能满足移动端开发的基本要求. 废话不多说,上代码 1.首先介绍一下,sqlite的管理类SQLi ...
- Ext JS 6开发实例(一)
很久没写文章了,主要原因和大家差不多,都要为生活奔忙,搞了两个小项目.这两个小项目很凑巧,都可以使用Ext JS来开发,这正是练习使用Ext JS 6的好机会,自然不会错过. 很多读者可能会问,为什么 ...
- Ruby开发入门
开发环境搭建 首先安装Ruby SDK,我安装的版本是2.0.之后安装IDE,这里用的是Jetbrain的RubyMine 5.4.3,注意是否支持对应版本的Ruby SDK. 一段神奇的注册码... ...
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...
- MyEclipse10+Jdk1.7+OSGI+MySql实现数据库的增删改查
开发环境: Windows2008R2 64位+MyEclipse10+jdk1.7.0_67+MySql5.5 软件安装:myeclipse-10.0-offline-installer-windo ...