方式1:借助栈 空间辅助度是O(N)

方式2: 借助栈 空间复杂度是 O(n/2)。只存后半个链表

方式3: 反转后半个链表  最后再反转回来

 package my_basic.class_3;

 import java.util.Stack;

 //是否是回文结构  121  1221,
public class Code_11_IsPalindromeList {
public static class Node{
int value;
Node next;
public Node(int value) {
super();
this.value = value;
}
} //need extra(n) space 栈辅助
public static boolean isPalindrome1(Node head) {
Stack<Node> stack = new Stack<Node>();
Node cur = head;
while(cur != null) {
stack.push(cur);
cur = cur.next;
}
while (head != null && !stack.empty()) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
if (head==null) {
System.out.println("head null");
}
return true;
} //need n/2 extra space 栈辅助
public static Boolean isPalindrome2(Node head) {
Stack<Node> stack = new Stack<Node>();
if (head == null || head.next == null) {
return true;
}
Node right = head.next;
Node cur = head;
if (cur.next!=null || cur.next.next !=null) {
cur = cur.next.next;
right = right.next;
}
while (right != null) {
stack.push(right);
right = right.next;
}
while(!stack.empty()) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
} //need O(1) extra space 反转半个链表
public static Boolean isPalindrome3(Node head) {
if (head == null || head.next == null) {
return true;
}
Node n1 = head;
Node n2 = head;
while (n2.next != null && n2.next.next!=null) {
n1 = n1.next; //mid
n2 = n2.next.next; //last
}
n2 = n1.next; //右边第一个节点
n1.next = null;
Node n3 = null;
while (n2 != null) { /*反转右半部分*/
n3 = n2.next;
n2.next = n1;
n1 = n2;
n2 = n3;
}
n3 = n1; //n3->last node
n2 = head;
boolean res = true;
while (n2 != null && n1 != null) {
if (n2.value != n1.value) {
// return false; 不能return 还要把链表反转回来
res = false;
break;
}
n2 = n2.next;
n1 = n1.next;
} n1 = n3.next;
n3.next = null;
while (n1 != null) { // recover list
n2 = n1.next;
n1.next = n3;
n3 = n1;
n1 = n2;
}
return res; } public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
} public static void main(String[] args) {
Node head = null;
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(2);
head.next.next.next = new Node(2);
// head.next.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.print(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("========================="); }
}

判断一个链表是否为回文结构 【题目】 给定一个链表的头节点head,请判断该链表是否为回 文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。 15->6->15,返回true。 1->2->3,返回false。 进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。的更多相关文章

  1. 链表回文判断(C++)

    题目描述: 对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构. 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构.保证链表长度小于等 ...

  2. 判断回文字符串、回文链表、回文数(python实现)

    所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...

  3. LeetCode 234:回文链表 Palindrome Linked List

    ​ 请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...

  4. LeetCode——回文链表

    题目 给定一个链表的头节点head,请判断该链表是否为回 文结构. 例如: 1->2->1,返回true. 1->2->2->1,返回true. 15->6-> ...

  5. Python生成一个不含回文字符串的字符串

    [本文出自天外归云的博客园] 回文字符串介绍 回文字符串就是对称的字符串,例如: “ABA” “ABBA” “ABCBA” 题目 给定一个字符串,请发明一种方法,让字符串中不包含回文字符串. 我的解法 ...

  6. Java 判断回文字符串有多少和其中的最大字符串

    一.简介代码功能 该代码的功能可以实现对任意的一段字符串进行判断是否有回文,回文有哪些,和其中的最大回文. 二.代码部分 1.全局变量 static String hws = "" ...

  7. 九度oj 题目1528:最长回文子串

    题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. 回文子串,顾名思义,即字符串中满足回文性质的子串. 给出一个只由小写英文字符a,b,c...x, ...

  8. [Jobdu] 题目1528:最长回文子串

    题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.回文子串,顾名思义,即字符串中满足回文性质的子串.给出一个只由小写英文字符a,b,c...x,y, ...

  9. C实例--推断一个字符串是否是回文数

    回文是指顺读和反读内容均同样的字符串.比如"121","ABBA","X"等. 本实例将编写函数推断字符串是否是回文. 引入两个指针变量,開 ...

随机推荐

  1. hdu3949XOR(线性基)

    传送门 不知道线性基是什么东西的可以看看蒟蒻的总结 题目大意:求一堆数字能异或出的第$k$大的数是多少 线性基求第k大好珂怕…… 据大佬们说就是把$k$给二进制拆分,如果$k$的第$i$位为1,那么$ ...

  2. 洛谷P2505 [HAOI2012]道路(最短路计数)

    传送门 早上模拟赛考这题,结果竟然看错题目了orz 然后下午看完题解自己做的时候空间开小了白WA了好久orz 首先,如果以$S$为起点,一条边$(u,v)$在最短路上,则$dis[u]+edge[i] ...

  3. Node.js 内置模块fs(文件系统)

    fs模块的三个常用方法 1.fs.readFile() -- 读文件 2.fs.writeFile() -- 写文件 3.fa.stat() -- 查看文件信息 fs模块不同于其它模块的地方是它有异步 ...

  4. builder模式的新学习

    builder模式的新学习 静态工厂和构造器有个共同的局限性:他们不能很好的扩展到大量的可选参数.大多数产品在牧歌可选与中都会有非零的值 对于这种类,应该使用哪种构造器或者静态方法来进行编写?程序员一 ...

  5. 小试JVM工具

    一.前言 工欲善其事必先利其器,jdk自带了很多工具,利用好这些工具能够帮我们获取想要的数据(运行日志.异常堆栈.GC日志.线程快照.堆转储快照等),从而快速的分析数据.定位问题. 二.jps:虚拟机 ...

  6. 初学Java web(转)

    转自 http://www.oschina.net/question/12_52027 OSCHINA 软件库有一个分类——Web框架,该分类中包含多种编程语言的将近500个项目. Web框架是开发者 ...

  7. Tomcat日志文件的输出在Linux和Windows下的差异

    前言 最近老大发现Tomcat的日志文件catalina.out里存在着大量的和公司项目相关的log信息,因为一般都是会使用日志框架并另外将log信息输出到另外的文件里的,catalina.out文件 ...

  8. Python-7-字典方法

    clear 删除所有字典项 >>> d = {} >>> d['name'] = 'Gumby' >>> d['age'] = 42 >&g ...

  9. VMware每次联网都需要还原默认设置解决办法

    参考:https://zhidao.baidu.com/question/553464573715382812.html

  10. JavaScript特点、优缺点及常用框架

    参考来源:   http://www.cnblogs.com/SanMaoSpace/archive/2013/06/14/3136774.html