leetcode234
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * public int val;
- * public ListNode next;
- * public ListNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public bool IsPalindrome(ListNode head) {
- if (head == null)
- {
- return true;
- }
- else
- {
- Queue<int> Q = new Queue<int>();
- Stack<int> S = new Stack<int>();
- do
- {
- Q.Enqueue(head.val);
- S.Push(head.val);
- head = head.next;
- }
- while (head != null);
- var len = S.Count;
- for (int i = ; i < len; i++)
- {
- var s = S.Pop();
- var q = Q.Dequeue();
- if (s != q)
- {
- return false;
- }
- }
- return true;
- }
- }
- }
https://leetcode.com/problems/palindrome-linked-list/#/description
补充一个python的实现:
- class Solution:
- def isPalindrome(self, head: ListNode) -> bool:
- lists = []
- while head != None:
- lists.append(head.val)
- head = head.next
- i,j = ,len(lists)-
- while i < j:
- if lists[i] != lists[j]:
- return False
- i +=
- j -=
- return True
leetcode234的更多相关文章
- 【LeetCode234】Palindrome Linked List★
题目描述: 解题思路: 判断一个单向链表是否是回文链表,并且要求O(n)的时间复杂度和O(1)的空间复杂度. 方法有以下几种: 1.遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回 ...
- leetcode234 回文链表 两种做法(stack(空间非O(1)),空间O(1))
link: leetcode234 回文链表 方法1, 快慢指针,把前半部分存入栈中和后半部分比较 public boolean isPalindrome(ListNode head) { if(he ...
- [LeetCode234]Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome. 判断一个单链表是不是回文 思路: 1.遍历整个链表,将链表每个节点的值 ...
- [Swift]LeetCode234. 回文链表 | Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode234 回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- <LC刷题二>回文字符串判断之leetcode125&234
其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
随机推荐
- 如何ASP.NET Core Razor中处理Ajax请求[转载]
在ASP.NET Core Razor(以下简称Razor)刚出来的时候,看了一下官方的文档,一直没怎么用过. 今天闲来无事,准备用Rozor做个项目熟练下,结果写第一个页面就卡住了..折腾半天才搞好 ...
- pthon入门之strip()和split()函数简单区分
小白,分享记录学习新感悟 路飞的第一次作业写一个登录的程序,作业的升级需求中有个锁文件的需求,大致上如果用户数错了密码三次将用户写到黑名单上,下次登录锁定: ok基本的要求写完,我们上代码 usern ...
- django 问题总结
1.更新了pip之后还提示更新 // 卸载 pip uninstall pip // 重新安装 easy_install pip pip -V 2.时间比当前时间少8小时问题 // 设置setting ...
- Java错误:结束的字符文字
编译器为NetBeans 在学习java的时候突然出现了以下错误 错误代码是: Gen <Integer ,String> a = new Gen <Integer, String& ...
- scrapy框架之递归解析和post请求
递归爬取解析多页页面数据 scrapy核心组件工作流程 scrapy的post请求发送 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析 ...
- mysql in 查询参数化
mysql查询语句where条件in mysql查询语句where条件in 正常情况需要查询的语句:select *from temp where id in ('1','2','3','4','5' ...
- 整理面试问题iOS
1.如何添加手势操作. 我们以在view上来举例 //创建一个view UIView *tapView=[UIView new]; tapView.frame=CGRectMake(, , kWidt ...
- 编写 python 小程序,将LOL官网的皮肤保存下来,上传百度云,记录那些强撸灰飞烟灭的日子
to 撸的血泪史:大学四年几乎都在宿舍打撸,So,把官网的皮肤都保存下来,存到百度云,就当一种纪念 编辑器:pycharm 用到的包:urllib.request, requests, json, r ...
- 洛谷P1605:迷宫(DFS)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...
- python gevent自动挡的协程切换。
import gevent def func(): print('running func 111')#第一步运行 gevent.sleep(2)#切换到下个协程 print('running fun ...