本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [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 ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [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 ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. Java基础之枚举妙用

    对于枚举,初学Java的时候可能我们就已经接触过了,但是在毕业前,其实一直都不知道真正工作里面枚举是怎么用的,枚举有什么用?接下来,博主就介绍枚举在实际工作中的一种使用场景,本文只适合初级的小菜鸟看哈 ...

  2. Android底部导航栏

    Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...

  3. 20 ViewPager总结

    V4:兼容到1.6,V7:兼容到 2.1,V13:兼容到3.2(平板) 兼容包 ViewPager 父类: android.support.v4.view.ViewPager ViewPager:滑动 ...

  4. CSDN发表文章后老是待审核的原因

    最近开始在csdn上写文章,发现老是文章被 待审核 ,于是便在网上搜集了下网友们的反馈,最后做出以下的整理. 1.CSDN检测到文章中的链接大于5,就会将文章列为"待审核",这个其 ...

  5. 早期Swift中Cocos2D初始化代码的重构

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在早期的Swift中在子类里只能调用超类的design ...

  6. [ExtJS5学习笔记]第十七节 Extjs5的panel组件增加accodion成为折叠导航栏

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39102335 官方例子:http://dev.sencha.com/ext/5.0.1 ...

  7. Struts2中的struts.multipart.saveDir的配置

    <constant name="struts.multipart.saveDir" value="D:\\AsimsTemp"></const ...

  8. THE SCHOOLS WHERE APPLE, GOOGLE, AND FACEBOOK GET THEIR RECRUITS

  9. 新手推荐:Hadoop安装教程_单机/伪分布式配置_Hadoop-2.7.1/Ubuntu14.04

    下述教程本人在最新版的-jre openjdk-7-jdk OpenJDK 默认的安装位置为: /usr/lib/jvm/java-7-openjdk-amd64 (32位系统则是 /usr/lib/ ...

  10. Java 多线程 死锁 隐性死锁 数据竞争 恶性数据竞争 错误解决深入分析 全方向举例

    在几乎所有编程语言中,由于多线程引发的错误都有着难以再现的特点,程序的死锁或其它多线程错误可能只在某些特殊的情形下才出现,或在不同的VM上运行同一个程序时错误表现不同.因此,在编写多线程程序时,事先认 ...