package class_03;import java.util.Stack;/*** * 判断一个链表是否为回文结构【题目】 给定一个链表的头节点head,请判断该链表是否为回文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。15->6->15,返回true。 1->2->3,返回false。进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1)。**/
public class Code_11_IsPalindromeList {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}// need n extra spacepublic 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) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;}// need n/2 extra spacepublic static boolean isPalindrome2(Node head) {if (head == null || head.next == null) {return true;}Node right = head.next;Node cur = head;while (cur.next != null && cur.next.next != null) {right = right.next;cur = cur.next.next;}Stack<Node> stack = new Stack<Node>();while (right != null) {stack.push(right);right = right.next;}while (!stack.isEmpty()) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;}// need O(1) extra spacepublic 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) { // find mid noden1 = n1.next; // n1 -> midn2 = n2.next.next; // n2 -> end}n2 = n1.next; // n2 -> right part first noden1.next = null; // mid.next -> nullNode n3 = null;while (n2 != null) { // right part convertn3 = n2.next; // n3 -> save next noden2.next = n1; // next of right node convertn1 = n2; // n1 moven2 = n3; // n2 move}n3 = n1; // n3 -> save last noden2 = head;// n2 -> left first nodeboolean res = true;while (n1 != null && n2 != null) { // check palindromeif (n1.value != n2.value) {res = false;break;}n1 = n1.next; // left to midn2 = n2.next; // right to mid}n1 = n3.next;n3.next = null;while (n1 != null) { // recover listn2 = 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;printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(2);head.next.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);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.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");}}

判断一个链表是否为回文结构相关推荐

  1. 算法练习day9——190327(“之” 字形打印矩阵、在行列都排好序的矩阵中找数、打印两个有序链表的公共部分、判断一个链表是否为回文结构)

    1."之" 字形打印矩阵 [题目] 给定一个矩阵matrix, 按照"之" 字形的方式打印这个矩阵, 例如: 1 2 3 4 5 6 7 8 9 10 11 1 ...

  2. 数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构

    数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构 目录 打印两个有序链表公共部分 判断一个链表是否具有回文结构 1. 打印两个有序链表公共部分 1.问题描述 思路:Node1和N ...

  3. 牛客题霸 [判断一个链表是否为回文结构] C++题解/答案

    判断一个链表是否为回文结构 题目描述 给定一个链表,请判断该链表是否为回文结构. 题解: 直接将链表内的数据存入string中,然后从两端开始向中间判断即可 代码: /*** struct ListN ...

  4. c语言数据结构判断回文数,C++数据结构与算法之判断一个链表是否为回文结构的方法...

    本文实例讲述了C++判断一个链表是否为回文结构的方法.分享给大家供大家参考,具体如下: 题目: 给定一个链表头节点head,请判断是否为回文结构 例如: 1->2->1 true 1-&g ...

  5. 判断一个链表是否为回文结构【Java实现】

    题目:给定一个链表的头节点head,请判断该链表是否为回文结构. 如:1 2 1 返回true 1 2 2 1 返回true 1 2 3 返回false 思路一: 利用栈,从左到右遍历链表,然后将每一 ...

  6. 刷题日记-判断一个链表是否为回文结构

    判断一个链表是否为回文结构 描述 给定一个链表,请判断该链表是否为回文结构. 回文是指该字符串正序逆序完全一致. 数据范围: 链表节点数 0≤n≤10510^5105,链表中每个节点的值满足∣val∣ ...

  7. (十三)判断一个链表是否是回文结构

    判断一个链表是否是回文结构 判断一个链表是否是回文结构 栈实现 栈+快慢指针 有限个变量 判断一个链表是否是回文结构 [题目]给定一个单链表的头结点,请判断该链表是否为回文结构 [要求]如果链表长度为 ...

  8. 判断一个链表是否为回文结构-Java:解法三

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击http://www.captainbed.net package live.every.day.Pro ...

  9. NC96 判断一个链表是否为回文结构

    NC96 判断一个链表是否为回文结构 描述 给定一个链表,请判断该链表是否为回文结构. 示例1 输入: [1] 复制 返回值: true 复制 示例2 输入: [2,1] 复制 返回值: false ...

最新文章

  1. python红包游戏_脑力2048红包版
  2. LeetCode之Reverse Integer
  3. Unity3D For Android 开发教程【转http://game.ceeger.com/Unity/Doc/2011/Unity3D_For_Android.html】...
  4. php 获取cookieid,Redis实现Session共享详解
  5. QT学习笔记(十四):QLayout的属性介绍
  6. Linux下 查看网络连接状态的命令是,查看Linux操作系统下的网络连接状态命令
  7. 将GPIO外设挂到Cortex_M3 AHB总线上详细流程扩展外设步骤总结
  8. 第二篇 Python数据类型、字符编码、文件处理
  9. mysql8从入门到精通电子书_MySQL 8从入门到精通(视频教学版)
  10. 信息系统项目管理师考试大纲(第2版)
  11. fastboot 操作
  12. 初中计算机数学,初中数学
  13. AttributeError: module ‘scipy.signal‘ has no attribute ‘correlation_lags‘
  14. r3 2200g参数 r3 2200g功耗 酷睿r32200g核显相当于什么显卡
  15. 搞机攻略(Android Root iOS越狱)
  16. 基于Raft共识协议的KV数据库
  17. 1000x计算机 案例解析,索尼WI-1000X耳机连接win10电脑方法讲解
  18. 【附源码】计算机毕业设计java在线音乐网站设计与实现
  19. 第九节 先电云openstack手动搭建创建云主机
  20. 【JVM】jvm虚拟机都有哪些?常用jvm虚拟机简介

热门文章

  1. 看到一个有意思的代码网页!!!!泡妹子的代码!!!
  2. 撤底理解es6中的箭头函数
  3. diea导入别人的项目遇到的一丢丢问题
  4. 【餐厅点餐平台|三】模块设计
  5. 最新百度网盘群组分享平台源码
  6. 问答精选|新年特辑:全方位揭秘MeterSphere一站式开源持续测试平台
  7. 通过用户名密码认证保障 MQTT 接入安全
  8. Unity3D C#数学系列之点积
  9. sybase iq load tabe语句5
  10. webpack之Scope Hoisting