• 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;
    }
    链表的结构和数量发生变化。
  • 相关阅读:
    vscode开发vue项目保存时自动执行lint进行修复
    React学习笔记-生命周期函数
    react 学习笔记
    iview的Affix组件滚动时没有按照预期固定
    iview input实现默认获取焦点并选中文字
    修改 浏览器滚动轴样式
    node连接mysql数据库
    mysql图形化管理工具workbench下载安装以及基本使用
    mysql 8.0版本下载安装以及默认密码修改
    节流和防抖 区别和实现
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7392666.html
Copyright © 2020-2023  润新知