首页 > 其他 > 详细

回文链表

时间:2019-05-26 20:01:23      阅读:113      评论:0      收藏:0      [点我收藏+]

leetcode地址:
https://leetcode.com/problems/palindrome-linked-list/description/

 

描述:

Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Follow up:
Could you do it in O(n) time and O(1) space?

 

要求时间复杂度O(n), 空间复杂度O(1)

 

解题分析:
这题的主要思路就是把链表分割,然后把右半部分反转,难点就在于分割,使用快慢指针,重点在于快指针走到尾部时的边界条件的处理需要分为偶数个还是奇数个情况,

如下图:

技术分享图片

可见,无论是奇数个元素还是偶数个元素的情况,当fast指针走到最后时,slow指针的下一个元素都是右半部分链表的开始,有了这个结论,我们就可以开始写代码了,

代码:

 

public boolean isPalindrome(ListNode head) {
if (null == head) {
return true;
}
// 切分链表
ListNode fast = head, slow = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode right = slow.next;
slow.next = null;
right = reverse(right);
while (right != null && head != null) {
if (right.val != head.val) {
return false;
}
right = right.next;
head = head.next;
}
return true;
}

public ListNode reverse(ListNode list) {
ListNode newHead = null;
while (list != null) {
ListNode next=list.next;
list.next = newHead;
newHead = list;
list = next;
}
return newHead;
}

显然,我们只是遍历了有限次的链表,因此时间复杂度是O(n);另外由于只是对链表进行反转,遍历等操作,并没有创建新的节点,所以占用空间是个常量,因此空间复杂度是O(1)

回文链表

原文:https://www.cnblogs.com/zhuge134/p/10927248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!