Answer: Basic logic, find the middle node, and reverse the right-side nodes, then compare left-side nodes with right-side nodes. Answer is from Jiuzhang: https://www.jiuzhang.com/solution/palindrome-linked-list/
/** * 本参考程序来自九章算法,由 @九章算法 提供。版权所有,转发请注明出处。 * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。 * - 现有的面试培训课程包括:九章算法班,系统设计班,算法强化班,Java入门与基础算法班,Android 项目实战班, * - Big Data 项目实战班,算法面试高频题班, 动态规划专题班 * - 更多详情请见官方网站:http://www.jiuzhang.com/?source=code */ // This code would destroy the original structure of the linked list. // If you do not want to destroy the structure, you can reserve the second part back. public class Solution { /** * @param head a ListNode * @return a boolean */ public boolean isPalindrome(ListNode head) { if (head == null) { return true; } ListNode middle = findMiddle(head); middle.next = reverse(middle.next); ListNode p1 = head, p2 = middle.next; while (p1 != null && p2 != null && p1.val == p2.val) { p1 = p1.next; p2 = p2.next; } return p2 == null; } private ListNode findMiddle(ListNode head) { if (head == null) { return null; } ListNode slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode temp = head.next; head.next = prev; prev = head; head = temp; } return prev; } }
No comments:
Post a Comment