leecode

盛水

leecode42,接雨水。动态规划or单调中栈or双指针。

leecode11,盛水,双指针

class Solution:
    def maxArea(self, height: List[int]) -> int:
        l,r = 0,len(height)-1
        ans = 0
        while lheight[r]:
                r -= 1
            else:
                l += 1
        return ans

leecode_第1张图片

leecode143,重排链表,找链表中间+反转链表+合并链表

class Solution:
    def reorderList(self, head: ListNode) -> None:
        if not head:
            return
        
        mid = self.middleNode(head)
        l1 = head
        l2 = mid.next
        mid.next = None
        l2 = self.reverseList(l2)
        self.mergeList(l1, l2)
    
    def middleNode(self, head: ListNode) -> ListNode:
        slow = fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def reverseList(self, head: ListNode) -> ListNode:
        prev = None
        curr = head
        while curr:
            nextTemp = curr.next
            curr.next = prev
            prev = curr
            curr = nextTemp
        return prev


    def mergeList(self, l1: ListNode, l2: ListNode):
        while l1 and l2:
            l1_tmp = l1.next
            l2_tmp = l2.next


            l1.next = l2
            l1 = l1_tmp


            l2.next = l1
            l2 = l2_tmp

二叉树

二叉搜索树28

二叉树的遍历94 144 145

 

你可能感兴趣的:(链表,数据结构)