leetcode——234. 回文链表

虽然效果不怎么样,但是好歹是自己做的,自己的孩子是不会嫌弃的,,追求改进,,,

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        new_head=None
        p=head
        while p:
            tmp=p.next
            a=ListNode(p.val)
            a.next=new_head
            new_head=a
            p=tmp     
        while head:
            if head.val!=new_head.val:
                return False
            head=head.next
            new_head=new_head.next
        return True
执行用时 :172 ms, 在所有 python3 提交中击败了5.06%的用户
内存消耗 :32.4 MB, 在所有 python3 提交中击败了5.09%的用户
 
 
执行用时为 52 ms 的范例
# 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:
            return True
        else:
            node = head
            val_list = []
            val_reverse = []
            while node:
                val_list.append(node.val)
                node = node.next
            val_reverse.extend(val_list)
            val_reverse.reverse()
            return  val_list == val_reverse

这样用列表做我当然也会做,不用列表呢???

——2019.10.24

 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11734991.html
今日推荐