方式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. swift4.0 方法监听Selector写法总结

    import UIKit class MainViewController: UITabBarController { //MARK:属性 懒加载 lazy var composeBtn = UIBu ...

  2. node-sass安装失败

    1. 直接安装报错(版本根据自己需求来) npm i node-sass@ -D 报错不能下载 win32-x64-64_binding.node Downloading binary from ht ...

  3. Codeforces Round #383 【总结】

    总结一下这场... 搞起大号就崩了... A题没啥问题..通过电脑到手机上提交没啥大问题: B题,讲个最严重的问题,也是最近以来最严重的问题,虽然我说你开了个数组,虽然这个数组是不是最终的答案数组,但 ...

  4. 点击a标签 跳到当前页面指定div

    给标签div设一个id值 <div id="aa"><div> a标签抓取id值,点击跳过去 <a href="#aa">& ...

  5. laravel MVC分布及数据库配置

    laravel MVC分布 M app\Http\Middleware V resources\views C app\Http\Controllers 数据库配置 目录   config\datab ...

  6. uoj#274. 【清华集训2016】温暖会指引我们前行(LCT)

    传送门 不难发现肯定是在温度的最大生成树上走是最优的 于是用\(LCT\)维护最大生成树,每一次加边时如果已经连通,就判断一下路径上的最小温度是否小于当前温度,是的话就断掉那条边,加入新边 //min ...

  7. P4091 [HEOI2016/TJOI2016]求和(第二类斯特林数+NTT)

    传送门 首先,因为在\(j>i\)的时候有\(S(i,j)=0\),所以原式可以写成\[Ans=\sum_{i=0}^n\sum_{j=0}^nS(i,j)\times 2^j\times j! ...

  8. vim 快速定位到文件末尾、头部

    gg           : 跳转到文件头 Shift+g   : 跳转到文件末尾 行数+gg : 跳转到指定行,例跳转到123行:123gg

  9. library not found for -lXXXXX 编译问题的解决方法

    喜欢交朋友的加:微信号 dwjluck2013 编译和真机调试的时候都正常 在打包的时候遇到这个问题 解决方案:pod install 重新下载就解决了  分享给遇到同类问题的小伙伴

  10. iOS蓝牙连接流程介绍-1

    蓝牙连接流程介绍 1.1-程序员找女朋友流程介绍 0.程序员找女朋友参与者 1.你 2.受害者(女性同胞)  (1)她的性格1 性格的特点 (2)她的性格2  分析性格的特点 1.寻找女性 寻尽身边一 ...