
python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def middleNode(self, head):
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
def reverseList(self, head):
pre, cur = None, head
while cur:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
return pre
def isPalindrome(self, head):
mid = self.middleNode(head)
head2 = self.reverseList(mid)
while head2:
if head.val != head2.val:
return False
head = head.next
head2 = head2.next
return True
解析:先找到中间位置,然后从中间反转, 再对比

python
class Solution(object):
def dailyTemperatures(self, temperatures):
length = len(temperatures)
ans = [0] * length
stack = []
for i in range(length):
temperature = temperatures[i]
while stack and temperature > temperatures[stack[-1]]:
pre_index = stack.pop()
ans[pre_index] = i - pre_index
stack.append(i)
return ans
解析:单调栈里面存的是下标,遇到大的就弹出来且记录,不大就继续存着