Palindrome Linked List leetcode 234

原创 2015年07月21日 09:31:36

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?

判断给定的链表是不是回文的:

把要判断的链表分成两部分,将第二部分逆置,逐个与前一部分的val相比较,若不同返回false

/**
 * 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* slow=head;//这里定义slow,fast指针是为了寻找链表中间的位置
        ListNode* fast=head;
        while(fast->next&&fast->next->next){//假如链表有奇数个元素,则slow指针在循环后指向正中的元素;
        //若链表有偶数个元素,则循环后slow指向n/2的元素位置
            slow=slow->next;
            fast=fast->next->next;
        }
        slow->next=reverse(slow->next);//将链表后面部分逆置
        slow=slow->next;
        while(slow){
            if(head->val!=slow->val)return false;
            head=head->next;
            slow=slow->next;
        }
        return true;
    }
    ListNode* reverse(ListNode* head){
        if(!head||!head->next)return head;
        ListNode* pre=NULL;
        ListNode* next=NULL;
        while(head){
            next=head-next;
            head->next=pre;
            pre=head;
            head=next;
        }
    return pre;
    }
};

贴个递归解法

bool isPalindrome(ListNode* head) {
    if (!head || !head->next) return true;
    bool flag = true;
    recur_pal(head, head->next, flag);
    return flag;
}
void recur_pal(ListNode *&head, ListNode *tail, bool &flag) {
    if (head == tail || head == tail->next) return;
    if (tail->next) {
        recur_pal(head, tail->next, flag);
    }
    if (head->val != tail->val) { flag  = false; return;}
        head = head->next;
}

[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) tim...

(LeetCode 234)Palindrome Linked List

Leetcode第234题目是Palindrome Linked List。题目的描述如下: Given a singly linked list, determine if it is a pali...

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 a...

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List Total Accepted: 69120 Total Submissions: 225579 Difficulty: Easy ...

<LeetCode OJ> 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129...

LeetCode 234. Palindrome Linked List(对称链表)

原题网址:https://leetcode.com/submissions/detail/58253344/ Given a singly linked list, determine if it...
  • jmspan
  • jmspan
  • 2016年04月06日 05:36
  • 532

leetcode No234. Palindrome Linked List

QuestionGiven a singly linked list, determine if it is a palindrome. 判断链表是否为回文Algorithm需要解决的问题: 1、...

234.[LeetCode]Palindrome Linked List

一个要求时间复杂度是 O(n),空间复杂度是 O(1) 的判断链表是否是回文的// 时间和空间复杂度都有限的情况下来做运算 // 还是很简单的,遍历一遍链表,使用两个数,正向和逆向的存储我们的链表(1...

[理解leetcode解法]234. Palindrome Linked List 回文链表

[理解leetcode解法]234. Palindrome Linked List 回文链表 Given a singly linked list, determine if it is a pal...

LeetCode-Easy部分标签为LinkedList 234. Palindrome Linked List

根据回文数定义和单链表特点,求单链表是否为回文链表。要求时间复杂度O(n),空间复杂度O(1)。 思路,将后半部分反转,与前半部分元素一一比较。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Palindrome Linked List leetcode 234
举报原因:
原因补充:

(最多只允许输入30个字)