Java最简单详细版
链表的回文结构
https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa
import java.util.*; /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class PalindromeList { public boolean chkPalindrome(ListNode head) { if(head == null){//没有节点 return false; } if(head.next == null){ return true;//只有一个头节点 } //1、找中间节点 ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null){ fast = fast.next.next; slow = slow.next; } //2、反转中间节点后面的节点 ListNode cur = slow.next; while(cur != null){ ListNode curNext = cur.next; cur.next = slow; slow = cur; cur = curNext; } //3、head从前往后走,cur从后往前走比较 while(head != null && slow != null){ if(head.val != slow.val){ return false; } if(head.next == slow){ return true; } head = head.next; slow = slow.next; } return true; } }#我的实习求职记录#