leetcode 234 回文链表(Palindrome Linked List) python 多种思路,最简代码

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''

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

Example 1:

Input: 1->2
Output: false
Example 2:

Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?

'''


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

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """

        # Approach one 转化为数据回文, 引入了额外的存储空间, O(n),O(n)
#         res = []
#         while head:
#             res.append(head.val)
#             head = head.next

#         for i in range(len(res)//2):
#             if res[i] != res[-(i+1)]:
#                 return False
#         return True

        # # Approach 1.5    用列表的性质比较
        # res = []
        # while head:
        #     res.append(head.val)
        #     head = head.next
        # return res == res[::-1]



        # Approach two 快慢指针,压栈比较,O(n),O(n/2)
#         res = []
#         fast = slow = head
#         while fast and fast.next:
#             fast = fast.next.next
#             res.insert(0,slow.val)
#             slow = slow.next

#         if fast:   # 处理链表长度为奇数的情况
#             slow = slow.next

#         for i in res:
#             if i != slow.val:
#                 return False
#             slow = slow.next
#         return True



        # Approach three  快慢指针,反转后半段,比较O(n),O(1)
#         fast,slow = head,head
#         while fast and fast.next:
#             fast = fast.next.next
#             slow = slow.next

#         if fast:   # 处理链表长度为奇数的情况
#             slow = slow.next
#         new_slow = self.reverseList(slow)

#         while new_slow and new_slow.val == head.val:
#             new_slow, head = new_slow.next, head.next
#         return not new_slow

#     def reverseList(self,head):
#         res = None
#         while head:
#             curr = head
#             head = head.next
#             curr.next = res
#             res = curr
#         return res

        # Approach 3.5 快慢指针找中点的同时,反转前半段链表
        fast,slow,res = head,head,None
        while fast and fast.next:
            fast = fast.next.next
            res, res.next ,slow = slow, res, slow.next
        if fast:   # 处理链表长度为奇数的情况
            slow = slow.next
        while slow and slow.val == res.val:
            slow, res = slow.next, res.next
        return not slow
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值