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 ...
随机推荐
- Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置
在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...
- 剑指Offer——回溯算法解迷宫问题(java版)
剑指Offer--回溯算法解迷宫问题(java版) 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路. 下面我们来详细讲一 ...
- 02_3中方式的反射,通过Class.forName获得Class对象,通过类.class获得字节码对象,通过类实例.getClass()的方式获得Class对象
反射中加载类: Java中有一个Class类用于代表某一个类的字节码 .class文件 对应Class //1 加载类 // java中Class代表一个类,但是到底代表哪个类要明确指出 ...
- 错误:One or more post-processing actions failed. Consult the OPP service log for details
今天在做采购出入库明细报表的时候,有的时候能正常打印,有的时候报 One or more post-processing actions failed. Consult the OPP serv ...
- Android 文件操作心得体会
android 的文件操作说白了就是Java的文件操作的处理.所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了.好了,话不多说,开始今天的正题吧. 先从一个小项目入 ...
- 轻松学习Asp.net中的控件
C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...
- mysql进阶(二十四)防御SQL注入的方法总结
防御SQL注入的方法总结 这篇文章主要讲解了防御SQL注入的方法,介绍了什么是注入,注入的原因是什么,以及如何防御,需要的朋友可以参考下. SQL注入是一类危害极大的攻击形式.虽然危害很大,但是防御却 ...
- (国内)完美下载Android源码Ubuntu版
今天写的文章莫名奇妙的没了,所以再重新写一篇. 首先,为了方便起见,我已经将系统更换成里Ubuntu,因为官方推荐使用这个Linux发行版.先来一张系统的截图: Ubuntu的版本是16.04(推荐用 ...
- 2015年CSDN博客排名第一名,何方神圣?
2015年CSDN博客排名第一名,何方神圣? 一.引子: 话说博主phphot,雄霸天下好多年. 俱往矣, 落花流水春去也. 斗转星移,江山易主. 详细可以参见下文: CSDN博客排名第一名,何许人也 ...
- C++ Primer 有感(标准库map类型)
map是键-值对的集合.map类型通常可以理解为关联数组:可以使用键作为下标获取一个值,正如内置数组一样.而关联的本质在于元素的值于某个特定的键相关联,而并非通过元素在数组中的位置获取. 1.map对 ...