"Leetcode" 234. Palindrome linked List (Python & C + +)

Source: Internet
Author: User
234. palindrome Linked List

Topic link 234.1 topic Description:

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

Follow up:
Could do it in O (n) Time and O (1) spaces? 234.2 Problem Solving ideas:

Train of thought one: To determine whether the linked list is a palindrome, find a vector to push the values in the chain, and then traverse the vector, Judge V[i]!= v[v.size ()-i-1] is equal, not equal, directly return false. End of traversal, returns TRUE.

Idea two: Use reverse linked list. The difference is not to reverse the entire linked list, only back to the text of the second half of the list. Then it is to determine where is the middle position of palindrome.

First of all, reverse linked list function. Set a node the pre is empty and is the starting node after the reverse list. The set node next is null to reverse the next node of the current node in the list. Traversal, as long as the head is not empty, first save the Head->next in the next node. Then Head->next points to the pre, and the head node is saved in the pre. The last head saves the next node. End of traversal, return to pre node, complete inversion.

Then say to judge the middle position of palindrome. Set a slow pointer slow, a fast pointer fast. Traversal, as long as fast->next and Fast->next->next not empty, then slow forward a step, fast forward two steps, slow = Slow->next,fast = fast->next-> Next, so that when the traversal condition is not satisfied and the traversal is over, the slow just refers to the middle position, and if the length is counted, just the middle, and the length is even, the first one in the middle.

Then say reverse the back half of the list. The slow->next begins to reverse, Slow->next = Reverselist (Slow->next), and then slow = Slow->next.

The last is to judge whether it is a palindrome. At this time, can traverse head and slow, to determine whether the two values are equal, not equal directly return false. Returns true after the traversal has ended. 234.3 C + + code:

1, thinking One code (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, the idea of the second code (26MS):

Class Solution110_1 {//set speed pointer, flip after half list 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 to 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 code:

1, thinking One code (142MS)

Class Solution (object):
    def ispalindrome (self, head): ""
        : Type Head:listnode
        : Rtype:bool
        "" "
        s=[] While
        head!=none:
            s.append (head.val)
            Head=head.next to
        i in range (len (s)/2):
            if S [I]!=s[len (s) -1-i]: return
                False to
        True

2, train of Thought two code (155MS)

class Solution1 (object): Def ispalindrome (self, head): "": Type Head:listno
        De: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.N
                Ext.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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.