92 lines
3.0 KiB
Java
92 lines
3.0 KiB
Java
/*
|
||
* The MIT License (MIT)
|
||
*
|
||
* Copyright © 2022 xrv <xrv@live.com>
|
||
*
|
||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
* of this software and associated documentation files (the "Software"), to deal
|
||
* in the Software without restriction, including without limitation the rights
|
||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
* copies of the Software, and to permit persons to whom the Software is
|
||
* furnished to do so, subject to the following conditions:
|
||
*
|
||
* The above copyright notice and this permission notice shall be included in
|
||
* all copies or substantial portions of the Software.
|
||
*
|
||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
* THE SOFTWARE.
|
||
*/
|
||
|
||
package io.github.ehlxr.algorithm.linkedlist;
|
||
|
||
/**
|
||
* 判断单链表是否是回文
|
||
*
|
||
* @author ehlxr
|
||
* @since 2022-12-03 23:11.
|
||
*/
|
||
public class Palindrome {
|
||
public static void main(String[] args) {
|
||
ListNode head = new ListNode(1);
|
||
head.next = new ListNode(2);
|
||
head.next.next = new ListNode(3);
|
||
head.next.next.next = new ListNode(2);
|
||
head.next.next.next.next = new ListNode(1);
|
||
System.out.println(head);
|
||
System.out.println(isPalindrome(head));
|
||
|
||
head = new ListNode(1);
|
||
head.next = new ListNode(2);
|
||
head.next.next = new ListNode(3);
|
||
head.next.next.next = new ListNode(3);
|
||
head.next.next.next.next = new ListNode(2);
|
||
head.next.next.next.next.next = new ListNode(1);
|
||
System.out.println(head);
|
||
System.out.println(isPalindrome(head));
|
||
|
||
}
|
||
|
||
/**
|
||
* slow, fast 双指针,slow 前进一步,fast 前进两步,当 fast 到达链表尾部时,slow 处于链表中间
|
||
* 与此同时反转链表前半部分(head 到中间节点部分)
|
||
*/
|
||
public static boolean isPalindrome(ListNode head) {
|
||
if (head == null) {
|
||
return false;
|
||
}
|
||
|
||
ListNode slow = head, fast = head, pre = null;
|
||
while (fast != null && fast.next != null) {
|
||
fast = fast.next.next;
|
||
|
||
ListNode tmp = slow.next;
|
||
slow.next = pre;
|
||
pre = slow;
|
||
slow = tmp;
|
||
// slow = slow.next;
|
||
}
|
||
if (fast != null) {
|
||
// 节点个数为奇数
|
||
slow = slow.next;
|
||
}
|
||
System.out.println("中间节点:" + slow);
|
||
System.out.println("中位点反转:" + pre);
|
||
|
||
while (pre != null) {
|
||
if (pre.val != slow.val) {
|
||
return false;
|
||
}
|
||
pre = pre.next;
|
||
slow = slow.next;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
}
|