zoukankan      html  css  js  c++  java
  • 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;
    }
    链表的结构和数量发生变化。
  • 相关阅读:
    forever让nodejs应用后台执行
    CentOS 程序开机自启动方法总结
    Centos7下配置Redis开机自启动
    Centos 关闭后台进程 .sh 等
    unity htc vive使用
    Linux登录验证机制、SSH Bruteforce Login学习
    Aho-Corasick算法、多模正则匹配、Snort入门学习
    The Honeynet ProjectThe Honeynet Project
    DEDECMS数据库执行原理、CMS代码层SQL注入防御思路
    PHP内核源代码、PHP Zend扩展、API Hook学习笔记
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7392666.html
Copyright © 2011-2022 走看看