判断一个链表是否为回文结构 【题目】 给定一个链表的头节点head,请判断该链表是否为回 文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。 15->6->15,返回true。 1->2->3,返回false。 进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。
方式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)。的更多相关文章
- 链表回文判断(C++)
题目描述: 对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构. 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构.保证链表长度小于等 ...
- 判断回文字符串、回文链表、回文数(python实现)
所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- LeetCode——回文链表
题目 给定一个链表的头节点head,请判断该链表是否为回 文结构. 例如: 1->2->1,返回true. 1->2->2->1,返回true. 15->6-> ...
- Python生成一个不含回文字符串的字符串
[本文出自天外归云的博客园] 回文字符串介绍 回文字符串就是对称的字符串,例如: “ABA” “ABBA” “ABCBA” 题目 给定一个字符串,请发明一种方法,让字符串中不包含回文字符串. 我的解法 ...
- Java 判断回文字符串有多少和其中的最大字符串
一.简介代码功能 该代码的功能可以实现对任意的一段字符串进行判断是否有回文,回文有哪些,和其中的最大回文. 二.代码部分 1.全局变量 static String hws = "" ...
- 九度oj 题目1528:最长回文子串
题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. 回文子串,顾名思义,即字符串中满足回文性质的子串. 给出一个只由小写英文字符a,b,c...x, ...
- [Jobdu] 题目1528:最长回文子串
题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.回文子串,顾名思义,即字符串中满足回文性质的子串.给出一个只由小写英文字符a,b,c...x,y, ...
- C实例--推断一个字符串是否是回文数
回文是指顺读和反读内容均同样的字符串.比如"121","ABBA","X"等. 本实例将编写函数推断字符串是否是回文. 引入两个指针变量,開 ...
随机推荐
- lightoj1062【几何(二分)】
其实就应该想到,哪有那么简单让你直接搞出答案的几何题啊:(而且很有可能是二分? 题意: 有两个梯子,一个靠在左边墙上,一个靠在右边墙上,长度分别为 x 和 y,他们的交点距离地面高度是 c,求两个梯子 ...
- HDU 1556【线段树区间更新】
这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...
- css div平移淡入淡出
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- bootstrap添加多个模态对话框支持
bootstrap添加多个模态对话框支持 (2015-03-04 21:05:35) 转载▼ 标签: 房产 因为项目需要,在页面交互上要弹出多个dialog窗口,而bootstrap的modal支 ...
- 【BZOJ4548】小奇的糖果
→原题传送门←(by Hzwer) 「题目背景」 小奇不小心让糖果散落到了地上,它对着满地的彩色糖果胡思乱想. 「问题描述」 有 N 个彩色糖果在平面上.小奇想在平面上取一条水平的线段,并拾起它上方或 ...
- Eclipse 修改编码方式
1.修改全局(Eclipse默认)文本编码方式 2.修改某个工程编码方式 右键工程点击”属性“后,如下图
- 51Nod 1134 最长递增子序列(动态规划O(nlogn))
#include <iostream> #include <algorithm> #include <stdio.h> #define MAXN 50010 usi ...
- [题解](树的计数)luogu_P4430猴子打架_/_luogu_P4981父子
来源:题解 比较不错的博客:http://www.cnblogs.com/dirge/p/5503289.html 最后生成一颗无根树,有n^(n-2)种情况,打架的顺序有(n-1)!种 #inclu ...
- [洛谷3935]Calculating
题目链接:https://www.luogu.org/problemnew/show/P3935 首先显然有\(\sum\limits_{i=l}^rf(i)=\sum\limits_{i=1}^rf ...