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;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(!head||!head->next){
            return true;
        }
        ListNode *p,*pre,*q;
        p=head;
        q=head;
        pre=NULL;
        while(q){
            q=q->next;
            if(!q){
                break;
            }
            pre=p;
            p=p->next;
            q=q->next;
        }
        pre->next=NULL;
        ListNode *r=reverse(p);
        p=head;
        q=r;
        while(p&&q){
            if(p->val!=q->val){
                return false;
            }
            p=p->next;
            q=q->next;
        }
        return true;

    }

    ListNode* reverse(ListNode *head){
        ListNode *h=NULL;
        ListNode *p=head;
        ListNode *tail=NULL;
        while(p){
            ListNode *next=p->next;
            p->next=NULL;
            if(!h){
                h=p;
            }else{
                p->next=h;
                h=p;
            }
            p=next;
        }
        return h;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 18:19:05

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 回文链表.

[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 par

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

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

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? 判断一个链表是否为回文串 思路:1.找到中间点,2.反转后半部分链表,3.判断前半部分与后半部分是否相同 /** * Definition for singly-linked list. * public class ListNode { * public int v