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. [LeetCode][JavaScript]Palindrome Linked List

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

  2. 【回文串7】LeetCode 234. Palindrome Linked List

    LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...

  3. 234. Palindrome Linked List - Easy

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  4. 234. Palindrome Linked List

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

  5. [LeetCode] Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 一开始想用栈,但是试来试去发现写不出来遂放弃,后来想想再不济可以转换成数组然后 ...

  6. LeetCode 234. Palindrome Linked List

    题目: Given a singly linked list, determine if it is a palindrome. 思路: 给定一个链表,判断它是不是回文链表 根据链表的奇偶分情况,然后 ...

  7. C#LeetCode刷题之#234-回文链表(Palindrome Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3905 访问. 请判断一个链表是否为回文链表. 输入: 1-> ...

  8. [LeetCode] 234. Palindrome Linked List_Easy tag: Linked List

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  9. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

最新文章

  1. centos /dev/mapper/cl-root 100% 解决方法
  2. 查看某个方法在哪里被调用_MATLAB局部函数公有化的方法: localfunctions
  3. JDK12的新特性:teeing collectors
  4. 有一个工程师男(女)朋友是什么样的体验?
  5. mysql时间排序_mysql – 按日期和时间降序排序?
  6. Redis进阶之持久化
  7. kb4023057安装失败_微软重发Win10 KB4023057 补丁,推动Win10更新
  8. VMWare的The network bridge on device VMnet0 is not running故障解决
  9. ORA-01033 ORACLE 正在初始化或关闭
  10. 使用Mac OS X如何开启和配置防火墙
  11. 【数据预测】基于matlab鸟群算法优化BP神经网络数据预测【含Matlab源码 1772期】
  12. cocosbuilder3.0使用小记
  13. OBS-RTMP推流
  14. T0.Games欢迎高端加密NFT入驻
  15. JQ使div动态拉伸,width
  16. Windows 系统优化大全(完整)
  17. Flutter CircularProgressIndicator进度指示器/Loading
  18. 《剑指offer》NO34 二叉树中和为某一值的路径 详解 <Java实现>
  19. 牛掰!SSH端口嗅探方法及其防御技术
  20. ubuntu14.04安装gnu/emacs24

热门文章

  1. 图形学教程Lecture 2: Review of Linear Algebra知识点总结
  2. spark1.0和2.0的区别_【系统】华为正式发布鸿蒙OS 2.0手机Beta版:UI界面、交互无区别...
  3. Openpose推断阶段原理
  4. JS-事件内置对象event
  5. LeakCanary——直白的展现Android中的内存泄露
  6. Servlet第二篇【Servlet调用图、Servlet细节、ServletConfig、ServletContext】
  7. lightoj1060_组合数学
  8. ios越狱系统UIGestureRecognizer事件截获问题
  9. C++数组参数应用方式探讨(转)
  10. (转)剖析Delphi中的构造和析构