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
        

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/84938998