[lintcode medium]Palindrome Linked List

Palindrome Linked List

Implement a function to check if a linked list is a palindrome.

Example

Given 1->2->1, return true

Challenge

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

////

1\find out the medium index of Linked list

2\ reverse the right part of linked list in place

3\compare their value;

/**
 * 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;
        if(head.next.next==null)
        {
            if(head.val==head.next.val)
            return true;

            else return false;
        }

        ListNode fast=head;
        ListNode slow=head;

        while(fast.next!=null && fast.next.next!=null )
        {
            fast=fast.next.next;
            slow=slow.next;
        }

        ListNode head2=slow.next;
        ListNode pre=null;
        ListNode cur=head2;

        slow.next=null;
        while(cur!=null)
        {
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }

        head2=pre;

        ListNode p=head;
        ListNode q=head2;
        while(q!=null)
        {
            if(p.val==q.val)
            {
                p=p.next;
                q=q.next;
            }
            else
             return false;
        }

    return true;

    }
时间: 2024-10-10 20:46:36

[lintcode medium]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 Linked List 回文链表.

leetCode 234. Palindrome Linked List 链表

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 and O(1) space? 题目大意: 判断一个单链表是否为回文链表. 思路: 找到链表中间的节点,将链表从中间分为2部分,右半部分进行链表反向转换,然后左半部分和反转后的右半部分链表进行比较.得出结果. 代码如下: /**  * Defi

<LeetCode OJ> 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129644 Difficulty: Easy 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? Subscribe to see

leetcode_234题——Palindrome Linked List(链表)

Palindrome Linked List Total Accepted: 5466 Total Submissions: 23472My Submissions Question Solution 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? Hide Tags Linked List Two Pointer

[LeetCode][JavaScript]Palindrome Linked List

Palindrome Linked List 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? https://leetcode.com/problems/palindrome-linked-list/ 判断单链表是否为回文,要求时间复杂度O(n),空间复杂度O(1). 如果对空间复杂度没有要求,有两种简单的做法.

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

Palindrome Linked List 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? 解法一: 一次遍历,装入vector,然后再一次遍历判断回文. 时间复杂度O(n),空间复杂度O(n) /** * Definition for singly-linked list. * struct ListNode

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) { v

Palindrome Linked List Leetcode

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? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

Palindrome Linked List

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? 思路: 把链表一分为二,把右边的一半翻转,再逐个比对左右的链表即可. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo