234. Palindrome Linked List

Posted Shihu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了234. Palindrome Linked List相关的知识,希望对你有一定的参考价值。

旋转链表函数

public ListNode reverse(ListNode head) {

  ListNode prev = null;

  while (head != null) {

    ListNode next = head.next;

    head.next = prev;

    prev = head;

    head = next;

   }

  return prev;

}

问题描述:

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

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

解题思路:将后半段的链表翻转,然后与前半段链表顺序比较。

问题:链表结构破坏能不能接受???边界条件,空和1? 空链表定义为回文吗?

我的解法:

/**
* Definition for singly-linked list.
* public class ListNode {
*   int val;
*    ListNode next;
*    ListNode(int x) { val = x; }
* }
*/
public class Solution {
  public boolean isPalindrome(ListNode head) {
    if (head == null || head.next == null) {
      return true;
    }
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode fast = dummy;
    ListNode slow = dummy;
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
    }
    ListNode middle = slow;
    ListNode postMiddle = slow.next;
    ListNode cur = slow.next.next;
    while (cur != null) {
      postMiddle.next = cur.next;
      cur.next = middle.next;
      middle.next = cur;
      cur = postMiddle.next;
    }
    slow = head;
    fast = middle.next;
    while (fast != null) {
      if (slow.val == fast.val) {
        slow = slow.next;
        fast = fast.next;
      } else {
        return false;
      }

/***优化一下

      if (slow.val != fast.val) {
        return false;
      }

      slow = slow.next;
      fast = fast.next;

***/

    }
    return true;
  }
}

借用了前面一道题,只是旋转了后半段链表,但是整个链表的数量没有变化,结构变化。

其他人的解法:

This can be solved by reversing the 2nd half and compare the two halves. Let‘s start with an example [1, 1, 2, 1].

In the beginning, set two pointers fast and slow starting at the head.

1 -> 1 -> 2 -> 1 -> null 
sf

(1) Move: fast pointer goes to the end, and slow goes to the middle.

1 -> 1 -> 2 -> 1 -> null 
          s          f

(2) Reverse: the right half is reversed, and slow pointer becomes the 2nd head.

1 -> 1    null <- 2 <- 1           
h                      s

(3) Compare: run the two pointers head and slow together and compare.

1 -> 1    null <- 2 <- 1             
     h            s
public boolean isPalindrome(ListNode head) {
    ListNode fast = head, slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
    }
    if (fast != null) { // odd nodes: let right half smaller
        slow = slow.next;
    }
    slow = reverse(slow);
    fast = head;
    
    while (slow != null) {
        if (fast.val != slow.val) {
            return false;
        }
        fast = fast.next;
        slow = slow.next;
    }
    return true;
}

public ListNode reverse(ListNode head) {
    ListNode prev = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = prev;
        prev = head;
        head = next;
    }
    return prev;
}
链表的结构和数量发生变化。

以上是关于234. Palindrome Linked List的主要内容,如果未能解决你的问题,请参考以下文章

234. Palindrome Linked List

LC.234.Palindrome Linked List

234. Palindrome Linked List

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List

234. Palindrome Linked List