当前位置: 首页 > news >正文

网站建设的目标是教学网页制作

网站建设的目标是,教学网页制作,台州手机网站开发,养老保险2023价格表问: 给定一个单链表的头节点head,请判断该链表是否为回文结构 例: 1 -> 2 -> 1返回true;1 -> 2 -> 2 -> 1返回true;15 -> 6 -> 15返回true 答: 笔试:初始化一个栈用来…

问:

给定一个单链表的头节点head,请判断该链表是否为回文结构

例:

1 -> 2 -> 1返回true;1 -> 2 -> 2 -> 1返回true;15 -> 6 -> 15返回true

答:

笔试:初始化一个栈用来存放链表中右半部分的元素(快慢指针),弹栈的顺序是链表的逆序

public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}
}
//额外空间n
public static boolean isPalindrome1 (Node head) {if (head == null || head.next == null) {return true;}Stack<Node> satck = 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;
}
//额外空间n/2,快慢指针
public static boolean isPalindrome2 (Node head) {if (head == null || head.next == null) {return true;}Node slow = head.next;Node fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}Stack<Node> stack = new Stack<Node>();//把后半段链表赋值到栈中while (slow != null) {stack.push(slow);slow = slow.next;}//将弹栈元素与前半段链表中元素对比while (!stack.isEmpty()) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;
}

面试:快慢指针找到终点位置,把右半个链表逆序,中点指向null,p1、p2分别为从左端、右端出发的指针,二者不断进行比对,最后恢复原来的结构

//额外空间1
public static boolean isPalindrome3 (Node head) {if (head == null || head.next == null) {return true;}//找到链表中点Node slow = head;Node fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}//反转链表的后半段fast = slow.next;slow.next = null;Node n = null;while (fast != null) {n = fast.next;fast.next = slow;slow = fast;fast = n;}//将前半段与后半段比较n = slow;fast = head;boolean res = true;while (slow != null && fast != null) {if (slow.value != fast.value) {res = false;break;}slow = slow.next;fast = fast.next;}//把链表恢复原样slow = n.next;n.next = null;while (slow != null) {fast = slow.next;slow.next = n;n = slow;slow = fast;}return res;
}
http://www.yayakq.cn/news/403035/

相关文章:

  • 全国工商核名查询系统官网泉州百度seo
  • 成都网站建设 小兵seo内容优化是什么
  • jsp做的网站运行都需要什么好玩网页传奇
  • 城市门户网站策划书迁移wordpress500错误
  • 网站建设小白到精通需要中山市西区建设局网站
  • 关注济南网站建设网络营销推广专员
  • 东莞阳光网站投诉平台公司手机网站效果图
  • seo优化或网站编辑黑群晖搭建wordpress外网访问
  • 免费的简历制作网站单页面视频网站模板
  • seo网站诊断价格深圳福田华丰大厦网站建设
  • 株洲建设工程造价信息网站苏州市建设局招标网站首页
  • 网站建设要多少钱app前端开发就是做网站吗
  • 网站建设实施过程互联网金融p2p网站建设
  • 网站幕布拍摄wordpress后台打开太慢
  • 做网站广告联盟赚钱国外做logo的网站
  • 临海市住房与城乡建设规划局网站电商平台商城系统搭建
  • 帝国手机网站cms系统二手房交易税费
  • 西安做网站的公司地址东莞定制网站建设
  • html网站设计作品丰镇市网站
  • 南京地区网站开发知名网站建设推广
  • 昆明网红自动优化网站建设电话
  • 网站百度排名怎么做快wordpress重装
  • 游戏类企业网站模板建设一个网站成本多少钱
  • 一个网站怎么做软件好用网站开发需要哪些技术人员
  • seo对于电子商务网站推广的作用兰州装修公司有哪些
  • 公司如何做网站推广中国十大服装设计院校
  • 定制网站开发公司哪家好?点击查看平面图设计网站
  • 深圳购物网站建企业形象通用网站
  • 做外贸网站有什么用可信网站 quot 验证能防范哪些安全.
  • 婴儿网站模板wordpress 局域网访问不了