zoukankan      html  css  js  c++  java
  • LeetCode OJ:Palindrome Linked List(回文链表判断)

    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?

    判断一个链表是否为回文链表,有很多方法都可以进行判断,可以先遍历链表然后将值全部存储到vector之中,也可以遍历链表然后将list中的值都压倒堆栈中再和List进行比较,

    但是想要达到题目上面的 时间复杂度为O(n),而空间复杂度为O(1)还要用到递归:

    首先是不用递归的方法(使用一个栈):

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     bool isPalindrome(ListNode* head) {
    12         ListNode * tmpHead = head;
    13         stack<int> lt;
    14         while (tmpHead){
    15             lt.push(tmpHead->val);
    16             tmpHead = tmpHead->next;
    17         }
    18 
    19         while (!lt.empty()){
    20             if (head->val != lt.top()) return false;
    21             lt.pop();
    22             head = head->next;
    23         }
    24         return true;
    25     }
    26 };

    java版本的代码:

     1 public class Solution {
     2     public boolean isPalindrome(ListNode head) {
     3         ListNode p = head;
     4         Stack<ListNode> s = new Stack<ListNode>();
     5         while (p != null) {
     6             s.push(p);
     7             p = p.next;
     8         }
     9         while(!s.isEmpty()){
    10             if(head.val != s.pop().val)
    11                 return false;
    12             head = head.next;
    13         }
    14         return true;
    15     }
    16 }

    首先上面这个空间复杂度是不满足要求的,再者,既然想到了将元素压入栈中,那么结合递归的话,应该就能做到空间复杂度O(1)了,应为上面的方法用到了堆栈,与递归能很好的结合:

     1 class Solution{
     2 private:
     3     ListNode * theHead;
     4 
     5 public:
     6     bool isPalindrome(ListNode* head) {
     7         theHead = head;  //先将head保存到一个theHead里面,便于与后面的递归了得指针相比较。
     8         return valid(head);
     9     }
    10 
    11     bool valid(ListNode * first)
    12     {
    13         if (first == NULL) return true;
    14         if (valid(first->next) == false) return false;
    15         if (first->val == theHead->val){
    16             theHead = theHead->next;
    17             return true;
    18         }
    19         else
    20             return false;
    21     }
    22 };

    因为道理比较简单,所以就不一一赘述了。

    java版本的代码如下所示:

     1 public class Solution {
     2     ListNode helper;
     3     public boolean isPalindrome(ListNode head) {
     4         helper = head;
     5         return isValid(head);
     6     }
     7 
     8     public boolean isValid(ListNode first){
     9         if(first == null) return true;
    10         if(isValid(first.next) == false) return false;
    11         if(first.val == helper.val){
    12             helper = helper.next;
    13             return true;
    14         }else
    15             return false;
    16     }
    17 }
  • 相关阅读:
    redhat安装opencv
    vsftpd的配置与使用
    Redhat 安装编译 Python-2.7.12
    YUM 安装与配置
    docker安装mysql
    高频问题 java8新特性(转载)
    quartz简单实例实现
    java8线程池
    java8多线程不带返回值
    java8多线程带返回值的
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4862236.html
Copyright © 2011-2022 走看看