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 ...
随机推荐
- activiti实战系列 并行网关(parallelGateWay)
流程图 13.2:部署流程定义+启动流程实例 13.3:查询我的个人任务 13.4:完成我的个人任务 说明: 1) 一个流程中流程实例只有1个,执行对象有多个 2) 并行网关的功能是基于进入和外出的 ...
- 协议系列之TCP/IP协议
根据前面介绍的几种协议,将IP协议.TCP协议.UDP协议组合起来,于是便有了TCP/IP协议.现在很多的应用的通信都是建立在TCP/IP协议的基础上,运用非常广泛,很有必要对其学习一下. 打个不太恰 ...
- React Native组件只Image
不管在Android还是在ios原生的开发中,图片都是作为控件给出来的,在RN中也有这么一个控件(Image).根据官网的资料,图片分为本地静态图片,网络图片和混合app资源.一下分类介绍来源官网. ...
- FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-PU
===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...
- Linux Debugging(八): core真的那么难以追踪吗?
本周遇到了好几个core都很有典型性.在这里和大家分享下. 相信有过Linux编程经验的人,肯定都遇到过.感觉周围人很多对core有天然的恐惧感,尤其对刚入行不久的同学来说.当然了,也有工作好几年看到 ...
- Dynamics CRM 将实体从高级查找列表中移除不可见
有时我们不需要将某个实体显示给一般用户比如配置实体,但是这种类型的实体有时候又需要给一般用户读权限ODATA的时候得能读,站点地图上的隐藏比较容易用工具配置下权限即可.其实做到这步一般就可以了但有的客 ...
- Servlet概述-servlet学习之旅(一)
Servlet概述 servlet是server+applet的缩写.applet是运行于客户端浏览器的java小程序,java诞生的时候,因为applet而闻名于世,但是现在已经没有多少热使用了,而 ...
- Java进阶(三十五)java int与integer的区别
Java进阶(三十五)java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象 ...
- 消息字节——MessageBytes
在tomcat核心处理中有这么一个需求--"为了提高编码性能,对于socket接收到的字节流不马上进行某种编码的转码,而是应该保留字节流的形式,在需要时.在指定编码时才进行转码工作" ...
- Devstack: A copy of worked local.conf I'm sharing with you.
service_plugins = neutron.services.firewall.fwaas_plugin.FirewallPlugin [service_providers] service_ ...