Python判断是否为回文链表

突然发现Python语法的优美和简洁,清爽,不拖泥带水。

龟叔(Guido van Rossum)就说:除了不能生孩子,python真的能干很多事。


# Definition for singly-linked list.
# 如果字节面试:时间复杂度O(n) ,空间复杂度O(1)

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


# 在规定的时间复杂度和空间复杂度的条件下来判断是否为回文链表。
class Solution():
	def isPalindrome(self,head)->bool:

		if head==None or head.next==None:
			return True

		dummy = ListNode(-1)
		dummy.next = head

		slow = dummy
		fast = dummy


		while fast and fast.next:
			slow = slow.next
			fast = fast.next.next


		fast = slow.next
		slow.next = None
		slow = dummy.next


		# 将后半段的链表逆置
		pre = None
		while fast:
			temp = fast.next
			fast.next =pre
			pre = fast
			fast = temp

		while pre:
			if pre.val!= slow.val:
				return False
			pre = pre.next
			slow = slow.next 
		return True



if __name__ == "__main__": 
	s = Solution()
	n2 = ListNode(1)
	n3 = ListNode(2)
	n4 = ListNode(1)
	n2.next = n3	
	n3.next = n4
	n4.next = None


	print(s.isPalindrome(n2))

 

你可能感兴趣的:(Python笔记)