leetcode summary: link list problem solved by extra space

143. Reorder List

link list ⇒ list

class Solution(object):
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: None Do not return anything, modify head in-place instead.
        """
        s=[]

        cur=head
        while cur:
            s.append(cur)
            cur=cur.next

        cnt=len(s)/2

        cur=head
        
        while cnt:
            node=s.pop()
            t=cur.next
            cur.next=node
            node.next=t

            cur=t
            cnt-=1

        if cur:
            cur.next=None
        return head

109. Convert Sorted List to Binary Search Tree

when not using extra space, we must seperate the link list again an again, find the middle node, then using recursive way to go on

class Solution(object):
    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """
        def helper(s):
            l=s
            h=s
            last=l
            if not s:
                return None
            if not s.next:
                return TreeNode(s.val)

            while h.next and h.next.next:
                h=h.next.next
                last=l
                l=l.next
            
            if l==h:
                l=l.next
        
            last.next=None
            h=l.next
            
            node=TreeNode(l.val)
            
            node.left=helper(s)
            node.right=helper(h)
            return node

        return helper(head)

we can using extra space to store all the list values to a array, so it change to the problem: convert array to binary search tree

class Solution:

    # Convert the given linked list to an array
    def mapListToValues(self, head):
        vals = []
        while head:
            vals.append(head.val)
            head = head.next
        return vals    

    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """

        # Form an array out of the given linked list and then
        # use the array to form the BST.
        values = self.mapListToValues(head)

        # l and r represent the start and end of the given array
        def convertListToBST(l, r):

            # Invalid case
            if l > r:
                return None

            # Middle element forms the root.
            mid = (l + r) // 2
            node = TreeNode(values[mid])

            # Base case for when there is only one element left in the array
            if l == r:
                return node

            # Recursively form BST on the two halves
            node.left = convertListToBST(l, mid - 1)
            node.right = convertListToBST(mid + 1, r)
            return node
        return convertListToBST(0, len(values) - 1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值