回文链表 牛客网 程序员面试金典  C++ Python

  • 题目描述
  • 请编写一个函数,检查链表是否为回文。
  • 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。
  • 测试样例:
  • {1,2,3,2,1}
  • 返回:true
  • {1,2,3,2,3}
  • 返回:false
class Palindrome {
public:
// run:4ms memory:492k
bool isPalindrome(ListNode* pHead) {
if(NULL == pHead) return true;
if(NULL == pHead->next) return true;
if(NULL == pHead->next->next)
if(pHead->val == pHead->next->val) return true;
else return false;
bool ret = true;
ListNode* lastNode = NULL;
ListNode* nextNode = pHead->next;
ListNode* pSlow = pHead;
ListNode* pFast = pHead;
while(pFast && pFast->next){
pFast = pFast->next->next;
pSlow->next = lastNode;
lastNode = pSlow;
pSlow = nextNode;
nextNode = nextNode->next;
}
ListNode* lNode = lastNode;
ListNode* nNode = pSlow;
if (NULL == pFast)
if (pSlow->val != lastNode->val) ret = false;
else lastNode = lastNode->next;
while(lastNode){
if (lastNode->val != nextNode->val) {
ret = false;
break;
} lastNode = lastNode->next;
nextNode = nextNode->next;
}
while(lNode){
ListNode* tmp = lNode->next;
lNode->next = nNode;
nNode = lNode;
lNode = tmp;
}
return ret;
} // run:5ms memory:600k
bool isPalindrome2(ListNode* pHead) {
if(NULL == pHead) return true;
if(NULL == pHead->next) return true;
if(NULL == pHead->next->next)
if(pHead->val == pHead->next->val) return true;
else return false;
ListNode* lastNode = NULL;
ListNode* nextNode = pHead->next;
ListNode* pSlow = pHead;
ListNode* pFast = pHead;
while(pFast && pFast->next){
pFast = pFast->next->next;
pSlow->next = lastNode;
lastNode = pSlow;
pSlow = nextNode;
nextNode = nextNode->next;
}
if (NULL == pFast)
if (pSlow->val != lastNode->val) return false;
else lastNode = lastNode->next;
while(lastNode){
if (lastNode->val != nextNode->val) return false;
lastNode = lastNode->next;
nextNode = nextNode->next;
}
return true;
} // run:4ms memory:476k
bool isPalindrome3(ListNode* pHead) {
if(NULL == pHead) return true;
if(NULL == pHead->next) return true;
if(NULL == pHead->next->next)
if(pHead->val == pHead->next->val)
return true;
else return false;
ListNode* lastNode = NULL;
ListNode* nextNode = pHead->next;
ListNode* pSlow = pHead;
ListNode* pFast = pHead;
while(pFast && pFast->next){
pFast = pFast->next->next;
pSlow->next = lastNode;
lastNode = pSlow;
pSlow = nextNode;
nextNode = nextNode->next;
}
if (NULL == pFast){
if (pSlow->val != lastNode->val)
return false;
else{
lastNode = lastNode->next;
while(lastNode){
if (lastNode->val != nextNode->val)
return false;
lastNode = lastNode->next;
nextNode = nextNode->next;
}
}
}
if(NULL == pFast->next){
while(lastNode){
if (lastNode->val != nextNode->val)
return false;
lastNode = lastNode->next;
nextNode = nextNode->next;
}
}
return true;
}
};

Python

# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Palindrome:
# run:43ms memory:5732k
def isPalindrome(self, pHead):
if None == pHead: return True
if None == pHead.next: return True
if None == pHead.next.next:
if pHead.val == pHead.next.val: return True
else: return False
ret = True
lastNode = None
nextNode = pHead
pFast = pHead
pSlow = pHead
while pFast and pFast.next:
pFast = pFast.next.next
nextNode = pSlow.next
pSlow.next = lastNode
lastNode = pSlow
pSlow = nextNode
nextNode = nextNode.next
lNode = lastNode
nNode = pSlow
if None == pFast:
if pSlow.val != lastNode.val:ret = False
else:lastNode = lastNode.next
while lastNode and nextNode:
if lastNode.val != nextNode.val:
ret = False
lastNode = lastNode.next
nextNode = nextNode.next
while lNode:
tmp = lNode.next
lNode.next = nNode
nNode = lNode
lNode = tmp
return ret # run:34ms memory:5728k
def isPalindrome2(self, pHead):
if None == pHead: return True
if None == pHead.next: return True
if None == pHead.next.next:
if pHead.val == pHead.next.val: return True
else: return False
lastNode = None
nextNode = pHead
pFast = pHead
pSlow = pHead
while pFast and pFast.next:
pFast = pFast.next.next
nextNode = pSlow.next
pSlow.next = lastNode
lastNode = pSlow
pSlow = nextNode
nextNode = nextNode.next
if None == pFast:
if pSlow.val != lastNode.val:return False
else:lastNode = lastNode.next
while lastNode:
if lastNode.val != nextNode.val:return False
lastNode = lastNode.next
nextNode = nextNode.next
return True

回文链表 牛客网 程序员面试金典 C++ Python的更多相关文章

  1. 链表分割 牛客网 程序员面试金典 C++ Python

    链表分割 牛客网 程序员面试金典 C++ Python 题目描述 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* p ...

  2. 链表中倒数第K个结点 牛客网 程序员面试金典 C++ Python

    链表中倒数第K个结点 牛客网 程序员面试金典 C++ Python 题目描述 输入一个链表,输出该链表中倒数第k个结点. C++ /* struct ListNode { int val; struc ...

  3. 链式A+B 牛客网 程序员面试金典 C++ Python

    链式A+B 牛客网 程序员面试金典 C++ Python 题目描述 有两个用链表表示的整数,每个结点包含一个数位.这些数位是反向存放的,也就是个位排在链表的首部.编写函数对这两个整数求和,并用链表形式 ...

  4. 输出单层结点 牛客网 程序员面试金典 C++ Python

    输出单层结点 牛客网 程序员面试金典 C++ Python 题目描述 对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表. 给定二叉树的根结点指针TreeNode* root,以及链表上 ...

  5. 访问单个结点的删除 牛客网 程序员面试金典 C++ Python

    访问单个结点的删除 牛客网 程序员面试金典 C++ Python 题目描述 实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点. 给定待删除的节点,请执行删除操作,若该节点为尾节点,返回f ...

  6. 平分的直线 牛客网 程序员面试金典 C++ Python

    平分的直线 牛客网 程序员面试金典 C++ Python 题目描述 在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分.假定正方形的上下两条边与x轴平行. 给定两个vecotrA和B ...

  7. 奇偶位交换 牛客网 程序员面试金典 C++ Python

    奇偶位交换 牛客网 程序员面试金典 C++ Python 题目描述 请编写程序交换一个数的二进制的奇数位和偶数位.(使用越少的指令越好) 给定一个int x,请返回交换后的数int. 测试样例: 10 ...

  8. 字符串压缩 牛客网 程序员面试金典 C++ Python

    字符串压缩 牛客网 程序员面试金典 C++ Python 题目描述 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能.比如,字符串"aabcccccaaa"经压缩会变 ...

  9. 双栈排序 牛客网 程序员面试金典 C++ Python

    双栈排序 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中. ...

随机推荐

  1. vue中的watch

    1.第一种 watch:{ total:{ // total:要检测的数据   handler:(val,oldVal)=>{ // handler方法自动执行   }, deep:true / ...

  2. CodeForce-807C Success Rate(二分数学)

    Success Rate CodeForces - 807C 给你4个数字 x y p q ,要求让你求最小的非负整数b,使得 (x+a)/(y+b)==p/q,同时a为一个整数且0<=a< ...

  3. Django学习day03随堂笔记

    每日测验 """ 今日考题 1.什么是静态文件,django静态文件配置如何配置,如何解决接口前缀不断变化,html页面上路径的引用需要反复修改的问题 2.request ...

  4. PHP设计模式之享元模式

    享元模式,"享元"这两个字在中文里其实并没有什么特殊的意思,所以我们要把它拆分来看."享"就是共享,"元"就是元素,这样一来似乎就很容易理解 ...

  5. Java基础系列(9)- 数据类型扩展及常见面试题

    整数拓展 // 整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x // 同一个数字在不同进制中,结果是不同的,进制换算 int i = 10; int i2 = 010; // 八进制 i ...

  6. 基于pgpool搭建postgressql集群部署

    postgresql集群搭建 基于pgpool中间件实现postgresql一主多从集群部署,这里用两台服务器作一主一从示例 虚拟机名 IP 主从划分 THApps 192.168.1.31 主节点 ...

  7. git 报错 gitThere is no tracking information for the current branch. Please specify which branch you w

    新建本地分支后将本地分支推送到远程库, 使用git pull 或者 git push 的时候报错gitThere is no tracking information for the current ...

  8. 使用jemeter构造各种变量数据

    使用手动创建测试数据太麻烦,因此考虑用jmeter写了一些创建测试数据的脚本,针对那些变量非固定的数据可以利用函数来实现 通过函数助手添加各种变量数据 Tools--->函数助手 1:生成当前时 ...

  9. centos7配置bind重启后错误解决

    最近研究centos7安装bind做DNS服务器,都配置好了后,重启后用systemctl status named 发现好多诸如以下错误:error (network unreachable) re ...

  10. 定要过python二级 第11套

    1. 2.乃至好的代码片段与解决方法,我保存在了 H:盘中python中的:H:\python\python二级好的代码片段与错误解决 3.接着第一个点,为什么print(read(f))  把f 放 ...