网站百度搜不到了,网页设计外文文献,展厅装修设计公司有,百度怎么把自己网站展现在百度问题背景
给你一个单链表的头节点 h e a d head head#xff0c;请你判断该链表是否为 回文链表#xff08;回文 序列是向前和向后读都相同的序列#xff09;。如果是#xff0c;返回 t r u e true true#xff1b;否则#xff0c;返回 f a l s e false false。
数据…问题背景
给你一个单链表的头节点 h e a d head head请你判断该链表是否为 回文链表回文 序列是向前和向后读都相同的序列。如果是返回 t r u e true true否则返回 f a l s e false false。
数据约束
链表中节点数目在范围 [ 1 , 1 0 5 ] [1, 10 ^ 5] [1,105] 内 0 ≤ N o d e . v a l ≤ 9 0 \le Node.val \le 9 0≤Node.val≤9
解题过程
非常经典的套路先用快慢指针求出链表的中间节点再从中间节点开始反转链表最后从头开始和反转后的链表比对是否相等即可。
虽然题目没有要求判断的题最好不要修改输入所以最后再把链表反转回来。
具体实现
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(int val, ListNode next) { this.val val; this.next next; }* }*/
class Solution {public boolean isPalindrome(ListNode head) {// 快慢指针求中间节点ListNode middle middle(head);// 从中间节点开始反转middle reverse(middle);ListNode check1 head, check2 middle; // 存一下两个指针防止丢失while(check2 ! null) {if(check1.val ! check2.val) {return false;}check1 check1.next;check2 check2.next;}// 把链表反转回来middle reverse(middle);return true;}private ListNode middle(ListNode head) {ListNode slow head, fast head;// 模板化的判断非空慢指针走一步同时快指针走两步最后慢指针会停在链表中点处while(fast ! null fast.next ! null) {slow slow.next;fast fast.next.next;}return slow;}private ListNode reverse(ListNode head) {// 初始化三个指针分别指向头节点和它的前后节点// 由于循环中会率先更新 next 指针这个变量可以不初始化ListNode pre null, cur head, next;while(cur ! null) {// 反转操作next cur.next;cur.next pre;// 轮转指针pre cur;cur next;}return pre; // 注意要返回 pre}
}
文章转载自: http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.snbq.cn.gov.cn.snbq.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn