【leetcode】234. Palindrome Linked List(Python & C++)

来源:互联网 发布:大话数据库 pdf 编辑:程序博客网 时间:2024/06/07 02:05

234. Palindrome Linked List

题目链接

234.1 题目描述:

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?

234.2 解题思路:

  1. 思路一:判断链表是否回文,找一个vector把链表里的值都一次push进去,然后遍历vector,判断v[i] != v[v.size() - i - 1]是否相等即可,不相等,直接返回false。遍历结束,返回true。

  2. 思路二:使用反转链表。不同的是不反转整个链表,只反转回文的后半段链表。然后就是判断出哪里是回文的中间位置。

    • 首先说反转链表函数。设置一个节点pre为空,是反转链表后的起始节点。设置节点next为空,是要反转链表当前节点的next节点。遍历,只要head不为空,则先将head->next保存在next节点中。然后head->next指向pre,然后head节点保存在pre中。最后head保存next节点。遍历结束,返回pre节点,即完成反转。

    • 然后说判断回文中间位置。设置一个慢指针slow,一个快指针fast。遍历,只要fast->next和fast->next->next不为空,则slow往前走一步,fast往前走两步,slow = slow->next,fast = fast->next->next,这样当不满足遍历条件、结束遍历时,slow刚好指在中间位置,如果长度是计数,则刚好中间,长度是偶数,则中间前一个。

    • 然后说反转后半部分链表。将slow->next开始反转,slow->next = reverselist(slow->next),然后将slow = slow->next。

    • 最后是判断是否是回文。这时,可同时遍历head和slow,判断二者值是否相等即可,不相等直接返回false。遍历结束后,返回true。

234.3 C++代码:

1、思路一代码(33ms):

class Solution110 {public:    bool isPalindrome(ListNode* head) {        vector<int>v;        while (head!=NULL)        {            v.push_back(head->val);            head = head->next;        }        for (int i = 0; i < v.size() / 2;i++)        {            if (v[i] != v[v.size() - i - 1])                return false;        }        return true;    }};

2、思路二代码(26ms):

class Solution110_1 {//设置快慢指针,翻转后一半列表public:    ListNode *reverselist(ListNode *head)    {        ListNode *pre = NULL;        ListNode *next = NULL;        while (head!=NULL)        {            next = head->next;            head->next = pre;            pre = head;            head = next;        }        return pre;    }    bool isPalindrome(ListNode* head) {        if (head == NULL || head->next == NULL)            return true;        ListNode *slow = head;        ListNode *fast = head;        while (fast->next!=NULL && fast->next->next!=NULL)        {            slow = slow->next;            fast = fast->next->next;        }        slow->next = reverselist(slow->next);        slow = slow->next;        while (slow!=NULL)        {            if (slow->val != head->val)                return false;            slow = slow->next;            head = head->next;        }        return true;    }};

234.4 Python代码:

1、思路一代码(142ms)

class Solution(object):    def isPalindrome(self, head):        """        :type head: ListNode        :rtype: bool        """        s=[]        while head!=None:            s.append(head.val)            head=head.next        for i in range(len(s)/2):            if s[i]!=s[len(s)-1-i]:                return False        return True

2、思路二代码(155ms)

class Solution1(object):    def isPalindrome(self, head):        """        :type head: ListNode        :rtype: bool        """        if head==None or head.next==None:            return True        slow=head        fast=head        while fast.next!=None and fast.next.next!=None:            slow=slow.next            fast=fast.next.next        def reverselist(head):            pre=None            ne=None            while head!=None:                ne=head.next                head.next=pre                pre=head                head=ne            return pre        slow.next=reverselist(slow.next)        slow=slow.next        while slow!=None:            if slow.val!=head.val:                return False            slow=slow.next            head=head.next        return True

原创粉丝点击