码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-234-回文链表

时间:2019-10-05 20:17:00      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:none   回文   image   nod   color   div   com   src   bsp   

题目描述;

技术图片

 

 方法一:O(N) O(1)

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

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        if not head or not head.next:return True
        slow = head
        fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        if fast:
            slow = slow.next
        prew = None
        cur = slow
        while cur:
            tmp = cur.next
            cur.next = prew
            prew = cur
            cur = tmp
        p1 = head
        p2 = prew
        while p1 and p2:
            if p1.val!=p2.val:return False
            p1 = p1.next
            p2 = p2.next
        return True
                

 

leetcode-234-回文链表

标签:none   回文   image   nod   color   div   com   src   bsp   

原文地址:https://www.cnblogs.com/oldby/p/11625440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!