Leetcode - Palindrome Linked List

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

[分析] 利用two pointers 方式找出链表的中间节点,接着翻转后半段链表,最后比较前后两半,如果完全相同则满足回文性质。注意到比较前后两半时以后半段长度为准,因为链表长度为奇数时,前半段会多一个节点。 Method2 是参考[url]http://blog.youkuaiyun.com/u013027996/article/details/46832437[/url]的实现,更加简洁,且不需要考虑长度奇数偶数问题,记slow初始标号为1, fast则为2,经过n次循环后,slow=1 + n, fast=2 + 2*n, 第一个while循环结束后,若input链表长度为偶数,则slow.next是后半段链表的起始节点,fast指向最后一个节点;若为奇数,则slow.next最终指向链表的中间节点,fast指向倒数第二个节点。最后判断两段链表是否相同时,若为奇数,则后半段会多一个,但不要紧,因为前半段遍历完while循环即结束。


// Method 2
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null) return true;
ListNode slow = head, fast = slow.next;
while (fast != null && fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode curr = slow.next; //p is the start of second half or the middle element
ListNode next = null;
ListNode head2 = null;
while (curr != null) {
next = curr.next;
curr.next = head2;
head2 = curr;
curr = next;
}
while (head != null && head2 != null) {
if (head.val != head2.val) return false;
head = head.next;
head2 = head2.next;
}
return true;
}
}



// Method 1
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null)
return true;
if (head.next.next == null)
return head.val == head.next.val ? true : false;
// split input list
ListNode p = head, q = head;
while (q != null) {
if (q.next != null) {
if (q.next.next != null) {
p = p.next;
q = q.next.next;
} else { // size is even
break;
}
} else { // size is odd
break;
}
}

// reverse second half
ListNode prev = null, curr = p.next, next = null;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}

// check if is palindrome
q = prev;
p = head;
while (q != null) {
if (p.val != q.val) return false;
p = p.next;
q = q.next;
}
return true;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值