lintcode 中等题:Palindrome Linked List 回文链表
题目
设计一种方式检查一个链表是否为回文链表。
1->2->1
就是一个回文链表。
O(n)的时间和O(1)的额外空间。
解题
法一:
再定义一个链表,存放链表反转的值,再以此比较两个链表中的值是否相等,时间复杂度O(N),空间复杂度O(N)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @return a boolean
*/
public boolean isPalindrome(ListNode head) {
// Write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
if(head == null || head.next == null)
return true;
ListNode p = head;
ListNode prev = new ListNode(head.val);
while(p.next != null){
ListNode tmp = new ListNode(p.next.val);
tmp.next = prev;
prev = tmp;
p = p.next;
}
ListNode p1 = head;
ListNode p2 = prev;
while(p1!=null){
if(p1.val != p2.val)
return false;
p1 = p1.next;
p2 = p2.next;
}
return true; }
}
Java Code
总耗时: 2219 ms
Python下面程序出现内存溢出
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def isPalindrome(self, head):
# Write your code here
if head == None or head.next == None:
return True
p = head
prev = ListNode(head.val)
while p.next!=None:
tmp = ListNode(p.next.val)
tmp.next = prev
prev = tmp
p = p.next
p1 = head
p2 = prev
del head
del prev
while p1!=None:
if p1.val != p2.val:
return False
p1 = p1.next
p2 = p2.next
return True
Python Code
法二:
先找到链表的中间节点,根据中间节点划分成两个链表,对第二个链表反转后在和第一个链表元素以此比较,但是下面的程序,在旋转的过程中空间复杂度好像是O(N/2) = O(N)
在找中间节点时候需要说明下
ListNode slow = head;
ListNode fast = head;
// 找到两个链表的中间节点
while( fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
}
开始时候两个节点都指向第一个节点,以后两个异步的向前走
最后结束的时候
当长度是奇数的时候:slow恰好在中间节点,fast恰好在最后一个节点。slow.next就是下一个链表的头节点。你会发现这两个链表是不一样的长的,但是在比较的时候,只要有一个链表是null的时候就结束比较的,也就是说只与第二个链表有关系的。
当长度是偶数的时候:slow在N/2的下取整处的节点,也就是中间两个节点的前一个,二fast在倒数第二个节点,下面就一样了
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @return a boolean
*/
public boolean isPalindrome(ListNode head) {
// Write your code here
if(head == null || head.next == null)
return true;
ListNode slow = head;
ListNode fast = head;
// 找到两个链表的中间节点
while( fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
}
ListNode secondHead = slow.next;
slow.next = null;
// 后半部分的节点反转
ListNode p1 = secondHead;
ListNode revsecondList = new ListNode(p1.val);
revsecondList.next = null;
while(p1.next != null){
ListNode tmp = new ListNode(p1.next.val);
tmp.next = revsecondList;
revsecondList = tmp;
p1 = p1.next;
} // 比较两个链表
while(head!=null && revsecondList!=null){
if(head.val != revsecondList.val)
return false;
head = head.next;
revsecondList = revsecondList.next;
}
return true;
}
}
Java Code
总耗时: 2229 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def isPalindrome(self, head):
# Write your code here
if head == None or head.next == None:
return True
slow = head
fast = head
while fast.next != None and fast.next.next != None:
fast = fast.next.next
slow = slow.next
secondHead = slow.next
slow.next = None
p = secondHead
rev = ListNode(p.val) while p.next!=None:
tmp = ListNode(p.next.val)
tmp.next = rev
rev = tmp
p = p.next while rev!=None and head!=None:
if rev.val != head.val:
return False
rev = rev.next
head = head.next
return True
Python Code
总耗时: 528 ms
在旋转链表的时候有重新定义了节点,如何只是修改节点而实现翻转,下面利用递归的思想翻转链表,但是在测试到97%的数据的时候运行时间超时。
public ListNode reverse(ListNode head){
if(head == null || head.next == null)
return head;
ListNode second = head.next;
head.next = null;
ListNode res = reverse(second);
second.next = head;
return res;
}
Java Code
下面是定义两个指针,第一个指向头节点,第二个指向头节点后一个节点
p1 = head
p2 = p1.next
while(p1!= null && p2!= null){
ListNode tmp = p2.next;
p2.next = p1;
p1 = p2;
p2 = tmp;
}
操作如下图所示
这里可以最后A还指向B的,可以在初始定义中增加
p1 .next = null
这样每次都是在p1节点之前增加一个节点的
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @return a boolean
*/
public boolean isPalindrome(ListNode head) {
// Write your code here
if(head == null || head.next == null)
return true;
ListNode slow = head;
ListNode fast = head;
// 找到两个链表的中间节点
while( fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
}
ListNode secondHead = slow.next;
slow.next = null;
// 后半部分的节点反转
ListNode p1 = secondHead;
ListNode p2 = p1.next;
p1.next = null; while(p1!= null && p2!= null){
ListNode tmp = p2.next;
p2.next = p1;
p1 = p2;
p2 = tmp;
}
secondHead.next = null;
revsecondList = p1;
// 比较两个链表
while(head!=null && revsecondList!=null){
if(head.val != revsecondList.val)
return false;
head = head.next;
revsecondList = revsecondList.next;
}
return true;
}
}
Java Code
总耗时: 2192 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def isPalindrome(self, head):
# Write your code here
if head == None or head.next == None:
return True
fast = head
slow = head
while fast.next!= None and fast.next.next!=None:
fast =fast.next.next
slow = slow.next
secondhead = slow.next
slow.next = None p1 = secondhead
p2 = p1.next
p1.next = None
while p1!=None and p2!=None:
tmp = p2.next
p2.next = p1
p1 = p2
p2 = tmp
rev = p1
while rev!=None and head!=None:
if rev.val!=head.val:
return False
rev = rev.next
head = head.next
return True
Python Code
总耗时: 516 ms
lintcode 中等题:Palindrome Linked List 回文链表的更多相关文章
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...
- [Swift]LeetCode234. 回文链表 | Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- LeetCode OJ:Palindrome Linked List(回文链表判断)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...
- 回文链表 · Palindrome Linked List
[抄题]: 设计一种方式检查一个链表是否为回文链表.1->2->1 就是一个回文链表. [暴力解法]: 时间分析: 空间分析: [思维问题]: 以为要从从后往前扫描,不知道调用revers ...
随机推荐
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- FastDFS4 + Ubuntu12安装及部署
1. 安装libevent 如果需要使用http进行下载,需要安装这个组件. 2. 安装fastdfs 3. 配置fastdfs 4. 安装nginx 问题4.1:安装nginx启动后访问原先资源UR ...
- 2013-07-29 IT 要闻速记快想
### ========================= ###传动视暴雪82亿美元赎身,腾讯参与投资 ### ========================= ###帮助企业解决打印&邮 ...
- php读取excel日期类型数据的例子
提供一个读取的函数: 代码如下 复制代码 //excel日期转换函数function excelTime($date, $time = false) { if(function_exists('Gr ...
- TClientDataSet中关于TField、TFieldDef动态创立字段的应用
//使用 TFieldDef 建表: begin with ClientDataSet1.FieldDefs do begin Add('Name' , ftString, 12, True); { ...
- 在有跳板机的情况下,SecureCRT自动连接到目标服务器
为了服务器的安全,运维人员经常会要求我们先登录到跳板机,然后再SSH连接到目标服务器.但是这样是很繁琐的,每次在SecureCRT创建一个连接,都需要输入SSH命令,然后输入密码. 下面的方法可以实现 ...
- Environment variable ORACLE_UNQNAME not defined. Please set ORACLE_UNQNAME to database unique name. 的解决方法
环境:Oracle 11g r2 win7 问题描述:Environment variable ORACLE_UNQNAME not defined. Please set ORACLE_UNQN ...
- java实现mysql数据库的备份及还原
备份: public static void backup() { try { Runtime rt = Runtime.getRuntime(); // 调用 调用mysql的安装目录的命令 Pro ...
- Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- bndtools教程
使用工具编程的确能给人们带来很多便利,但是在不会用之前,且缺乏相应的中文资料让你去了解时,真是一种折磨,同时也是一种挑战. bndTools其实就是用来开发OSGi的一个工具,它为开发提供了便利,具体 ...