题目:

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

链接: http://leetcode.com/problems/palindrome-linked-list/

题解:

判断链表是否是Palindrome。 我们分三步解,先用快慢指针找中点,接下来reverse中点及中点后部,最后逐节点对比值。

Time Complexity - O(n), Space Complexity - O(1)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null)
return true;
ListNode mid = findMid(head);
ListNode tail = reverse(mid);
mid.next = null; while(head != null && tail != null) {
if(head.val != tail.val)
return false;
else {
head = head.next;
tail = tail.next;
}
} return true;
} private ListNode findMid(ListNode head) { // find mid node of list
if(head == null || head.next == null)
return head;
ListNode slow = head, fast = head; while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
} return slow;
} private ListNode reverse(ListNode head) { // reverse listnode
if(head == null || head.next == null)
return head;
ListNode dummy = new ListNode(-1);
while(head != null) {
ListNode tmp = head.next;
head.next = dummy.next;
dummy.next = head;
head = tmp;
}
return dummy.next;
}
}

二刷:

和一刷一样,先快慢指针找重点,然后reverse后半部分,接下来遍历两个head逐个对比节点的值。最后返回true.

Java

Time Complexity - O(n), Space Complexity - O(1)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode mid = findMid(head);
ListNode reversedMid = reverse(mid);
ListNode node = head;
while (node != null && reversedMid != null) {
if (node.val != reversedMid.val) {
return false;
}
node = node.next;
reversedMid = reversedMid.next;
}
return true;
} private ListNode findMid(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
} private ListNode reverse(ListNode head) {
ListNode dummy = new ListNode(-1);
ListNode next = null;
while (head != null) {
next = head.next;
head.next = dummy.next;
dummy.next = head;
head = next;
}
return dummy.next;
} }

三刷:

跟二刷一样

Java:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) return true;
ListNode mid = findMid(head);
ListNode reversedMid = reverse(mid);
ListNode node = head;
while (node != null && reversedMid != null) {
if (node.val != reversedMid.val) return false;
node = node.next;
reversedMid = reversedMid.next;
}
return true;
} private ListNode findMid(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
} private ListNode reverse(ListNode head) {
ListNode dummy = new ListNode(-1);
ListNode next = null;
while (head != null) {
next = head.next;
head.next = dummy.next;
dummy.next = head;
head = next;
}
return dummy.next;
} }

Update:

这样写确实会破坏原来链表的结构。而且反转后半部分的时候是否可以可以算作O(1) space也值得商榷

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) return true;
ListNode mid = findMid(head);
ListNode tailReversed = reverse(mid);
while (tailReversed != null && head != null) {
if (tailReversed.val != head.val) return false;
tailReversed = tailReversed.next;
head = head.next;
}
return true;
} private ListNode findMid(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
} private ListNode reverse(ListNode head) {
ListNode dummy = new ListNode(-1);
ListNode tmp = null;
while (head != null) {
tmp = head.next;
head.next = dummy.next;
dummy.next = head;
head = tmp;
}
return dummy.next;
}
}

Reference:

https://leetcode.com/discuss/44751/11-lines-12-with-restore-o-n-time-o-1-space

234. Palindrome Linked List的更多相关文章

  1. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  2. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  3. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  4. 【LeetCode】234. Palindrome Linked List (2 solutions)

    Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...

  5. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  6. (easy)LeetCode 234.Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  7. Java [Leetcode 234]Palindrome Linked List

    题目描述: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) ...

  8. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

  9. [LeetCode] 234. Palindrome Linked List 解题思路

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

随机推荐

  1. C# 使用隐式或显示实现接口的区别

    通俗的来讲,"显示接口实现"就是使用接口名称作为方法名的前缀;而传统的实现方式称之为:"隐式接口实现".费话不说,例子如下:      interface IA ...

  2. Jquer Ajax xmlhttp请求成功了,为什么一直在error函数里面

    转载自http://www.cnblogs.com/sky_Great/archive/2013/01/18/2866861.html 并进行整理: 今天遇到了一个极其奇怪的问题,用各种工具检查都能看 ...

  3. Eclipse下jad反编译之“类文件查看器”不能处理给定的输入错误解决

    Eclipse中的插件下载,安装和配置可以参考我的另一篇文章:MyEclipse反编译Class文件 下面重点讲解如何使用jad反编译 1.在DOS窗口中,到class所在目录,直接运行 >ja ...

  4. mysql 连接语句

    在 SELECT 语句中,如果 FROM 子句引用了多个表源或视图,可以使用 JOIN 指示指定的联接操作应在指定的表源或视图之间执行. 一.交叉联接:CROSS JOIN 交叉联接将执行一个叉积(迪 ...

  5. matlab实现共轭梯度法、多元牛顿法、broyden方法

    共轭梯度法: function [ x, r, k ] = CorGrant( x0, A, b ) x = x0; r = b - A * x0; d = r; X = ones(length(x) ...

  6. MonoBehaviour

    所有的Unity脚本都继承自MonoBehaviour这个类,它没有Main函数入口,采用了事件触发的模式,根据不同的事件响应不同的函数. void Start(): void Update():每一 ...

  7. 高级php面试题及部分答案

    在网上看到一些高级php 的面试题目.. 闲来无事,搞了一些答案...可能不是很全面,留这以后备用吧. 一. 基本知识点1.1 HTTP协议中几个状态码的含义:503 500 401 403 404 ...

  8. 微软职位内部推荐-Senior SDE for Windows App Experience

    微软近期Open的职位: Job posting title: Senior Software Development Engineer Location: China, Beijing Divisi ...

  9. Notes of the scrum meeting(11/1)

    meeting time:9:00~10:30p.m.,November 1st,2013 meeting place:20号公寓楼前 attendees: 顾育豪                   ...

  10. 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】

    转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...