前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【leetcode 简单】 第六十七题

【leetcode 简单】 第六十七题

作者头像
py3study
发布2020-01-19 14:48:06
2180
发布2020-01-19 14:48:06
举报
文章被收录于专栏:python3python3

请判断一个链表是否为回文链表。

示例 1:

代码语言:javascript
复制
输入: 1->2
输出: false

示例 2:

代码语言:javascript
复制
输入: 1->2->2->1
输出: true

进阶: 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

代码语言:javascript
复制
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverst(self,head):
        a=head
        b=head.next
        head.next = None;
        while b:
            c = b.next
            b.next = a
            a = b
            b=c
        head = a
        return head
        
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if not head or not head.next:
            return True
    
        fast = slow = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        slow = slow.next
        slow = self.reverst(slow)
        while slow:
            if head.val != slow.val:
                return False
            slow = slow.next
            head = head.next
        return True
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档