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

推断一个链表是不是回文的,一个比較简单的办法是把链表每一个结点的值存在vector里。然后首尾比較。时间复杂度O(n)。空间复杂度O(n)。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> temp;
ListNode* ptr = head;
while(ptr!=NULL)
{
temp.push_back(ptr->val);
ptr = ptr->next;
}
int n = temp.size();
for(int i = 0; i < n/2; i++)
{
if(temp[i] != temp[n-1-i])
return false;
}
return true;
}
};

Leetcode47: Palindrome Linked List的更多相关文章

  1. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  2. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  3. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  4. 【LeetCode】234. Palindrome Linked List (2 solutions)

    Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...

  5. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  6. LeetCode_234. Palindrome Linked List

    234. Palindrome Linked List Easy Given a singly linked list, determine if it is a palindrome. Exampl ...

  7. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  8. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  9. Leetcode Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

随机推荐

  1. 对比学习sass和stylus的常用功能

    在众多的css预处理器语言中,sass和stylus算是十分优秀的两个.本文主要针对两者的常用功能做个简单的对比分析.在对比中了解二者的差异,同时帮助大家更好的掌握这两种预处理语言.本文涉及到的sas ...

  2. 题解 P1198 【[JSOI2008]最大数】

    说起来这还是蒟蒻AC的第一道省选线段树呢. 这道题和其他线段树最大的不同就是在于本题数组一直在增大. 寻常的线段树蒟蒻习惯用如下的结构体储存,然而对于此题就不行了: struct node{ int ...

  3. ECNUOJ 2619 询问

    询问 Time Limit:2000MS Memory Limit:65536KBTotal Submit:286 Accepted:70 Description  Pollux最近对字符串匹配很感兴 ...

  4. CodeForces 363B Fence

    Fence Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...

  5. java钩子函数

    也不知道我理解的对不对,欢迎大家讨论! 自己写了个钩子函数,我理解的钩子函数: public interface Transactioner { String wedontknow() ; } pub ...

  6. Android中System.currentTimeMillis()

    函数: System.currentTimeMillis(): 功能:产生一个当前的毫秒,这个毫秒事实上就是自1970年1月1日0时起的毫秒数,Date()事实上就是相当于Date(System.cu ...

  7. Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

    1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  8. jQuery插件开发初探

    最简单的插件 $.fn.changeStyle = function (colorStr) { $(this).css('color',colorStr); } 应用如下: <!DOCTYPE ...

  9. 常见的C字符串处理函数的源代码

    #include <stdio.h> #include <assert.h> char *strcpy(char *strDest,const char *strSrc) // ...

  10. Python(七) 高级部分:面向对象

    一.类的定义 # 面向对象 #有意义的面向对象代码 # 类 = 面向对象 # 类.对象 #实例化 # 类最基本的作用:封装 class Student(): name = '' age = 0 def ...