1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * public int val;
  5. * public ListNode next;
  6. * public ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public bool IsPalindrome(ListNode head) {
  11. if (head == null)
  12. {
  13. return true;
  14. }
  15. else
  16. {
  17. Queue<int> Q = new Queue<int>();
  18. Stack<int> S = new Stack<int>();
  19.  
  20. do
  21. {
  22. Q.Enqueue(head.val);
  23. S.Push(head.val);
  24.  
  25. head = head.next;
  26. }
  27. while (head != null);
  28.  
  29. var len = S.Count;
  30.  
  31. for (int i = ; i < len; i++)
  32. {
  33. var s = S.Pop();
  34. var q = Q.Dequeue();
  35. if (s != q)
  36. {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42. }
  43. }

https://leetcode.com/problems/palindrome-linked-list/#/description

补充一个python的实现:

  1. class Solution:
  2. def isPalindrome(self, head: ListNode) -> bool:
  3. lists = []
  4. while head != None:
  5. lists.append(head.val)
  6. head = head.next
  7. i,j = ,len(lists)-
  8. while i < j:
  9. if lists[i] != lists[j]:
  10. return False
  11. i +=
  12. j -=
  13. return True

leetcode234的更多相关文章

  1. 【LeetCode234】Palindrome Linked List★

    题目描述: 解题思路: 判断一个单向链表是否是回文链表,并且要求O(n)的时间复杂度和O(1)的空间复杂度. 方法有以下几种: 1.遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回 ...

  2. leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))

    link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...

  3. [LeetCode234]Palindrome Linked List

    题目: Given a singly linked list, determine if it is a palindrome. 判断一个单链表是不是回文 思路: 1.遍历整个链表,将链表每个节点的值 ...

  4. [Swift]LeetCode234. 回文链表 | Palindrome Linked List

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

  5. LeetCode234 回文链表

    请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...

  6. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  7. <LC刷题二>回文字符串判断之leetcode125&234

    其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...

  8. LeetCode通关:听说链表是门槛,这就抬脚跨门而入

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master       https://github.com/ch ...

随机推荐

  1. 如何ASP.NET Core Razor中处理Ajax请求[转载]

    在ASP.NET Core Razor(以下简称Razor)刚出来的时候,看了一下官方的文档,一直没怎么用过. 今天闲来无事,准备用Rozor做个项目熟练下,结果写第一个页面就卡住了..折腾半天才搞好 ...

  2. pthon入门之strip()和split()函数简单区分

    小白,分享记录学习新感悟 路飞的第一次作业写一个登录的程序,作业的升级需求中有个锁文件的需求,大致上如果用户数错了密码三次将用户写到黑名单上,下次登录锁定: ok基本的要求写完,我们上代码 usern ...

  3. django 问题总结

    1.更新了pip之后还提示更新 // 卸载 pip uninstall pip // 重新安装 easy_install pip pip -V 2.时间比当前时间少8小时问题 // 设置setting ...

  4. Java错误:结束的字符文字

    编译器为NetBeans 在学习java的时候突然出现了以下错误 错误代码是: Gen <Integer ,String> a = new Gen <Integer, String& ...

  5. scrapy框架之递归解析和post请求

    递归爬取解析多页页面数据 scrapy核心组件工作流程 scrapy的post请求发送 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析 ...

  6. mysql in 查询参数化

    mysql查询语句where条件in mysql查询语句where条件in 正常情况需要查询的语句:select *from temp where id in ('1','2','3','4','5' ...

  7. 整理面试问题iOS

    1.如何添加手势操作. 我们以在view上来举例 //创建一个view UIView *tapView=[UIView new]; tapView.frame=CGRectMake(, , kWidt ...

  8. 编写 python 小程序,将LOL官网的皮肤保存下来,上传百度云,记录那些强撸灰飞烟灭的日子

    to 撸的血泪史:大学四年几乎都在宿舍打撸,So,把官网的皮肤都保存下来,存到百度云,就当一种纪念 编辑器:pycharm 用到的包:urllib.request, requests, json, r ...

  9. 洛谷P1605:迷宫(DFS)

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...

  10. python gevent自动挡的协程切换。

    import gevent def func(): print('running func 111')#第一步运行 gevent.sleep(2)#切换到下个协程 print('running fun ...