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 ...
随机推荐
- dmp文件导入
dmp文件导入 1,打开“开始”-->输入cmd—> sqlplus log: 2,输入 conn / as sysdba管理员账户登录: 3,需要创建表空间,如果我们知道需要导入的数据库 ...
- MQTT开发小记(一)
最近在协助公司硬件组进行MQTT协议的嵌入式SDK包开发. 简述一下MQTT MQTT简单的来说是一种订阅/发布模式的通信形式,一般分为客户端和服务器端. MQTT服务器端可以简单理解为一个消息中转站 ...
- silverlight将字符串转化为控件
silverlight的System.Windows.Markup命名空间下,提供了XamlReader.Load()方法可以将字符串转换为控件. 代码 StringBuilder sbGrid = ...
- C# 汉字转拼音(转)
(一)将汉字转化成全拼代码: private void button1_Click(object sender, EventArgs e) { this.textBox2.Text = Hz2Py.C ...
- 更改windows服务的配置文件app.config
/// <summary> /// 获取每次处理记录数 /// </summary> /// <returns></returns> private s ...
- Z-BlogPHP 安装出现 (8) Undefined offset: 6 解决方法
有些cp面板的空间会在每个网页头部和页脚增加两个调用的文件,导致zblogPHP安装出错:(8) Undefined offset: 6 主要国外的主机中PHP配置文件两个选项auto_prepend ...
- vs2012用wpf制作透明窗口中报错的解决方案
在开发wpf项目时,需要调用外部com组件,同时需要制作透明窗口,于是问题出现了,当我们在设置 AllowsTransparency="True"后,com组件显示不出来了,只有透 ...
- linux 目录
创建和删除 int mkdir(const char *pathname, mode_t mode); int rmdir(const char *pathname); 另外remove也可以删除文件 ...
- ESB概述
ESB概念的提出: 企业有大量的异构的业务系统,这些系统之间需要通过web service来交互数据,这样的话可以想象系统直接的交互关系是蜘蛛网状的,是难以维护的.这时候提出了ESB,每个业务系统只需 ...
- iOS 进阶 第十二天(0413)
0413 - Quartz2D 使用Quartz2D画图形步骤:(drawRect: 方法 是 在view第一次显示到屏幕上的时候会调用一次.是系统主动去掉用的,不能直接调用.那么问题来了,比如我要通 ...