1. 程式人生 > >LeetCode 234. 迴文連結串列(C、C++、python)

LeetCode 234. 迴文連結串列(C、C++、python)

請判斷一個連結串列是否為迴文連結串列。

示例 1:

輸入: 1->2
輸出: false

示例 2:

輸入: 1->2->2->1
輸出: true

進階:
你能否用 O(n) 時間複雜度和 O(1) 空間複雜度解決此題?

思路:1)先找到中間節點; 2)將中間節點後面的部分反轉; 3)將反轉的部分與中間節點前的部分比較

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverse(struct ListNode* head)
{
	if(NULL==head || NULL==head->next)
	{
	    return head;
	}
	struct ListNode* pcur=head;
	struct ListNode* pnew=NULL;
	struct ListNode* pnext=NULL;
	while(pcur)
	{
	    pnext=pcur->next;
	    pcur->next=pnew;
	    pnew=pcur;
	    pcur=pnext;
	}
	return pnew;
}
bool isPalindrome(struct ListNode* head) 
{
    if(NULL==head || NULL==head->next)
    {
        return true;
    }
    struct ListNode* fast=head;
    struct ListNode* slow=head;
    while(fast->next!=NULL && fast->next->next!=NULL)
    {
        fast=fast->next->next;
        slow=slow->next;
    }
    slow=slow->next;
    slow=reverse(slow);
    while(slow)
    {
        if(slow->val!=head->val)
        {
            return false;
        }
        slow=slow->next;
        head=head->next;
    }
    return true;
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverse(ListNode* head)
    {
        if(NULL==head || NULL==head->next)
        {
            return head;
        }
        ListNode* pcur=head;
        ListNode* pnext=NULL;
        ListNode* pnew=NULL;
        while(pcur)
        {
            pnext=pcur->next;
            pcur->next=pnew;
            pnew=pcur;
            pcur=pnext;
        }
        return pnew;
    }
    bool isPalindrome(ListNode* head) 
    {
        if(NULL==head || NULL==head->next)
        {
            return true;
        }
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast->next!=NULL && fast->next->next!=NULL)
        {
            slow=slow->next;
            fast=fast->next->next;
        }
        slow=slow->next;
        ListNode* tmp=reverse(slow);
        while(tmp)
        {
            if(tmp->val!=head->val)
            {
                return false;
            }
            tmp=tmp->next;
            head=head->next;
        }
        return true;
    }
};

python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rev(self, head):
        if None==head or None==head.next:
            return head
        pcur=head
        pnew=None
        pnext=None
        while pcur:
            pnext=pcur.next
            pcur.next=pnew
            pnew=pcur
            pcur=pnext
        return pnew    
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if None==head or None==head.next:
            return True
        fast=head
        slow=head
        while fast.next!=None and fast.next.next!=None:
            fast=fast.next.next
            slow=slow.next
        slow=slow.next
        slow=self.rev(slow)
        while slow:
            if slow.val!=head.val:
                return False
            head=head.next
            slow=slow.next
        return True