回文链表
来源:互联网 发布:淘宝女装上下架时间 编辑:程序博客网 时间:2024/06/08 04:49
题目描述
请编写一个函数,检查链表是否为回文。
给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。
测试样例:
{1,2,3,2,1}
返回:true
{1,2,3,2,3}
返回:false
import java.util.*;/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Palindrome { public boolean isPalindrome(ListNode pHead) { // write code here if(pHead==null){ return false; } int len = 0; ListNode node = pHead; while(node!=null){ ++len; node = node.next; } if(len==1) return true; Stack<Integer> stack = new Stack<Integer>(); int n = len/2; node = pHead; for(int i=0; i<n; ++i){ stack.push(node.val); node = node.next; } if(len%2==1){ //链表元素个数为奇数个 node = node.next; } Integer tmp = null; while(node!=null&&!stack.isEmpty()){ tmp = stack.pop(); if(tmp!=node.val) return false; node = node.next; } return true; }}
0 0
- 字符串回文判断, 链表回文判断
- 链表回文判断
- 回文链表
- 回文链表(Java)
- 回文链表
- 回文链表
- 回文链表
- 判断回文链表
- 回文链表
- LintCode:回文链表
- 回文链表
- 回文链表
- 回文链表
- 回文链表
- 回文链表
- 链表回文
- LintCode:回文链表
- lintcode,回文链表
- 判断两个链表是否相交,若相交,求交点。(假设链表不带环)(C语言)
- 基数排序
- const用法
- 判断两个链表是否相交,若相交,求交点。(假设链表可能带环)【升级版】(C语言)
- Oracle VM + centos7.1+openstack kilo 多结点安装教程---keystone的安装(2)
- 回文链表
- const常见用法
- 单向链表
- 求链表相交时的交点(C语言)
- Linux make 命令与 makefile 文件
- 数据挖掘之距离计算算法
- JAVA实现有理数的运算+JAVA中的适配器模式
- IoCreateSymbolicLink函数的作用以及符号链接、设备名称之间的关系
- 神经网络学习(不定期更新)