回文链表 牛客网 程序员面试金典 C++ Python
回文链表 牛客网 程序员面试金典 C++ Python
- 题目描述
- 请编写一个函数,检查链表是否为回文。
- 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。
- 测试样例:
- {1,2,3,2,1}
- 返回:true
- {1,2,3,2,3}
- 返回:false
class Palindrome {
public:
// run:4ms memory:492kbool 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:600kbool 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:476kbool 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 = Noneclass Palindrome:
# run:43ms memory:5732kdef isPalindrome(self, pHead):if None == pHead: return Trueif None == pHead.next: return Trueif None == pHead.next.next:if pHead.val == pHead.next.val: return Trueelse: return Falseret = TruelastNode = NonenextNode = pHeadpFast = pHeadpSlow = pHeadwhile pFast and pFast.next:pFast = pFast.next.nextnextNode = pSlow.nextpSlow.next = lastNodelastNode = pSlowpSlow = nextNodenextNode = nextNode.nextlNode = lastNodenNode = pSlowif None == pFast:if pSlow.val != lastNode.val:ret = Falseelse:lastNode = lastNode.nextwhile lastNode and nextNode:if lastNode.val != nextNode.val:ret = FalselastNode = lastNode.nextnextNode = nextNode.nextwhile lNode:tmp = lNode.nextlNode.next = nNodenNode = lNodelNode = tmpreturn ret# run:34ms memory:5728kdef isPalindrome2(self, pHead):if None == pHead: return Trueif None == pHead.next: return Trueif None == pHead.next.next:if pHead.val == pHead.next.val: return Trueelse: return FalselastNode = NonenextNode = pHeadpFast = pHeadpSlow = pHeadwhile pFast and pFast.next:pFast = pFast.next.nextnextNode = pSlow.nextpSlow.next = lastNodelastNode = pSlowpSlow = nextNodenextNode = nextNode.nextif None == pFast:if pSlow.val != lastNode.val:return Falseelse:lastNode = lastNode.nextwhile lastNode:if lastNode.val != nextNode.val:return FalselastNode = lastNode.nextnextNode = nextNode.nextreturn True
回文链表 牛客网 程序员面试金典 C++ Python相关推荐
- 原串反转 牛客网 程序员面试金典 C++ Python
原串反转 牛客网 程序员面试金典 C++ Python 题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniS ...
- 空格替换 牛客网 程序员面试金典 C++ Python
空格替换 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个方法,将字符串中的空格全部替换为"%20".假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实 ...
- 集合栈 牛客网 程序员面试金典 C++ Python
集合栈 牛客网 程序员面试金典 C++ Python 题目描述 请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈.该数据结构应支持与普通 ...
- 双栈排序 牛客网 程序员面试金典 C++ Python
双栈排序 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中. ...
- 整数转化 牛客网 程序员面试金典 C++ Python
整数转化 牛客网 程序员面试金典 C++ Python 题目描述 编写一个函数,确定需要改变几个位,才能将整数A转变成整数B. 给定两个整数int A,int B.请返回需要改变的数位个数. 测试样例 ...
- 确定字符互异 牛客网 程序员面试金典 C++ Python
确定字符互异 牛客网 程序员面试金典 C++ Python 题目描述 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniStri ...
- 无缓存交换 牛客网 程序员面试金典 C++ Python
无缓存交换 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个函数,函数内不使用任何临时变量,直接交换两个数的值. 给定一个int数组AB,其第零个元素和第一个元素为待交换的值,请返回 ...
- 平衡二叉树检查 牛客网 程序员面试金典 C++ Python
平衡二叉树检查 牛客网 程序员面试金典 C++ Python 题目描述 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1. 给定指向树根结点的指针T ...
- 平分的直线 牛客网 程序员面试金典 C++ Python
平分的直线 牛客网 程序员面试金典 C++ Python 题目描述 在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分.假定正方形的上下两条边与x轴平行. 给定两个vecotrA和B ...
最新文章
- 网站ui的配色有哪些需要注意的?
- getcwd()和dirname(__FILE__)的区别
- 易语言逐条读access数据_易语言对ACCESS数据库基础(适合新手)
- Nutch使用方法简介
- 虚拟机中那些可以作为root对象_java虚拟机面试题及答案整理
- Python学习5 元组基础知识和常用函数
- Cookie禁用了Session还可以用吗?
- OPENQUERY用法以及使用需要注意的地方
- 属于哪个单元_1~6年级上册数学第一单元知识整理
- 农业智慧物联卡火爆发展中存在的问题
- php readfile cookie,python处理cookie详解
- 配置IIS5.5/6.0 支持 Silverlight
- win10 激活 错误码 0xc004f074
- txt转mobi格式
- Q4实现盈利,搜狐迎来“疫”外春天?
- WordPress主题的最佳市场:TemplateMonster还是ThemeForest?
- html做微信logo,HTML5新特性之用SVG绘制微信logo
- 红米4 android os唤醒,红米4(Redmi 4 高配版)一键救砖教程,轻松刷回官方系统
- 小小白的Android入门之计算器学习
- 如何快速的将EXCEL表格数据拆分成多个文件