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 ...
随机推荐
- centos7 操作防火墙
原文:https://blog.csdn.net/u012498149/article/details/78772058 1.firewalld的基本使用 启动: systemctl start fi ...
- Vue - v-for 的延伸用法
1.v-for 合并标签template 一起使用 2.vue.set 1.v-for 合并标签template 一起使用 之前在设计table的时候,如果使用v-for ,会直接放在tr里面,一次产 ...
- groupmod语法
语法 groupmod [-g <群组识别码> <-o>][-n <新群组名称>][群组名称] 参数: -g <群组识别码> 设置欲使用的群组识别码. ...
- ECB cspk7 加密
public function test(){ $param = input('param.'); // $input = 'userid=7&gameid=100107&buycou ...
- element-ui Select 清空model,页面没有清空选中项的问题
业务场景: 在dialog 每次打开时, 选择应用程序要初始化为空. 最初的做法为: 监听dialog的show状态,当show为false时,设置selectApp为空这样写时,虽然selectAp ...
- python int str
1. int 类型转换 a = "123" b = int(a) b = b+10 print(type(a),a) print(type(b),b) 2. int(num,bas ...
- ieee trans pami latex模板
https://www.computer.org/cms/Computer.org/transactions/templates/ https://www.computer.org/web/tpami ...
- A Language Modeling Approach to Predicting Reading Difficulty-paer
Volume:Proceedings of the Human Language Technology Conference of the North American Chapter of the ...
- python 在一个excel存多个sheet
import pandas as pdimport numpy as npimport osfrom sqlalchemy import create_engine def get_station_w ...
- 14.python-CS编程
一.客户端/服务器架构1.C/S架构:(1)硬件C/S架构(打印机)(2)软件C/S架构(web服务)2.生活中的C/S架构:饭店是S端,所有食客是C端3.C/S架构与socket的关系:socke就 ...