判断列表是否回文 时间复杂度O(N) 额外空间复杂度O(1)

生成随机长度链表

时间复杂度O(N) 额外空间复杂度O(1)的解

N:链表长度(问题的规模)
通过快慢指针找到中点节点 遍历N
反转中点右边节点的指向 遍历0.5N
判断是否回文 遍历0.5N
恢复中点右边的节点的指向 0.5N
总的需要遍历链表2.5N次

def isPalindrome(head):
    if not head or not head.next:
        return True
    # 快慢指针
    slow = fast = head
    while fast.next and fast.next.next:
        slow = slow.next
        fast = fast.next.next
    leftHead = head
    rightHead = None
    cur = slow
    while cur:
        pre = cur
        cur = cur.next
        pre.next = rightHead
        rightHead = pre
    # 保存右边的头节点
    cur = rightHead
    while leftHead:
        if leftHead.val != rightHead.val:
            return False
        leftHead = leftHead.next
        rightHead = rightHead.next
    # 恢复链表
    post = None
    while cur:
        pre = cur
        cur = cur.next
        pre.next = post
        post = pre
    return True
优化

通过快慢指针找中点的时候,反转中点左边的节点的指向,判断是否回文的时候就反转中点左边节点的指向。这种方式需要判断链表长度的奇偶性。
优化后需要遍历1.5N

def isPalindrome1(head):
    if not head or not head.next:
        return True
    if not head.next.next:
        if head.val != head.next.val:
            return False
        else:
            return True
    odd = True
    slow = head
    leftHead = None
    fast = head.next
    while fast.next and fast.next.next:
        preslow = slow
        slow = slow.next
        preslow.next = leftHead
        leftHead = preslow
        fast = fast.next.next
        if not fast.next:
            odd = False
    slowNext = rightHead = slow.next
    if odd:
        rightHead = slow.next.next
    slow.next = leftHead
    leftHead = slow
    while leftHead:
        if leftHead.val != rightHead.val:
            return False
        # 恢复链表指向
        preslow = leftHead
        leftHead = leftHead.next
        preslow.next = slowNext
        slowNext = preslow
        rightHead = rightHead.next
    return True
对数器(验证)
def varifyIsPalindrome(head):
    if not head or not head.next:
        return True
    lst = []
    cur = head
    while cur:
        lst.append(cur.val)
        cur = cur.next
    for i in range(len(lst)//2):
        if lst[i] != lst[len(lst)-1-i]:
            return False
    return True
测试
def test(num=10000):
    for i in range(num):
        head = generateLinkedList(20)

        res2 = varifyIsPalindrome(head)
        res1 = isPalindrome2(head)

        if res1 is not res2:
            print("varifyIsPalindrome Failed")
            traverseLinkedList(head)
            print()
            print("isPalindrome", res1)
            print("varifyIsPalindrome", res2)
            break
    else:
        print("nice!")

if __name__ == '__main__':
    test()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值