Beispiel #1
0
def qucik_sort_list(head):
    def partition(start, end):
        node = start.next.next
        pivotPrev = start.next
        pivotPrev.next = end
        pivotPost = pivotPrev
        while node != end:
            temp = node.next
            if node.val > pivotPrev.val:
                node.next = pivotPost.next
                pivotPost.next = node
            elif node.val < pivotPrev.val:
                node.next = start.next
                start.next = node
            else:
                node.next = pivotPost.next
                pivotPost.next = node
                pivotPost = pivotPost.next
            node = temp
        return [pivotPrev, pivotPost]

    def quicksort(start, end):
        if start.next != end:
            prev, post = partition(start, end)
            quicksort(start, prev)
            quicksort(post, end)

    newHead = ListNode(0)
    newHead.next = head
    quicksort(newHead, None)
    return newHead.next
Beispiel #2
0
 def reverse(p):
     newHead = ListNode('')
     while p:
         q = p
         p = p.next
         q.next = newHead.next
         newHead.next = q
     return newHead.next
Beispiel #3
0
def mergesort_List(head: ListNode) -> ListNode:
    def merge(head, q, qk, k):
        left = right = k
        while left and right and qk:
            if q.val > qk.val:
                head.next = qk
                qk = qk.next
                right -= 1
            else:
                head.next = q
                q = q.next
                left -= 1
            head = head.next
        head.next = None
        while left and q:
            head.next = q
            q = q.next
            head = head.next
            left -= 1
        while right and qk:
            head.next = qk
            qk = qk.next
            right -= 1
            head = head.next
        head.next = qk
        return head

    k, length = 1, 0
    p = head
    while p:
        length += 1
        p = p.next

    newHead = ListNode('')
    newHead.next = head

    while k < length:
        head = newHead
        while head.next:
            q = qk = head.next
            for i in range(k):
                if not qk:
                    break
                qk = qk.next
            head = merge(head, q, qk, k)
        k *= 2
    return newHead.next
Beispiel #4
0
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL
'''
class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head:
            return head
        p=head
        i=1
        while i<=k:
            if not p.next:
                k=k%i
                i=1
                p=head
            else:
                p=p.next
                i+=1
        q=head
        if k == 0:
            return head
        while p.next:
            p=p.next
            q=q.next
        newHead=q.next
        q.next=None
        p.next=head
        return newHead
head=ListNode.create_list([1])
print(Solution().rotateRight(head,1))
Beispiel #5
0
 def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
     if l1 and l2:
         if l1.val > l2.val: l1, l2 = l2, l1
         l1.next = mergeTwoLists(l1.next, l2)
     return l1 or l2
Beispiel #6
0
        p = q = head
        while q and q.next:
            pre = p
            p = p.next
            q = q.next.next
        if q:
            pre = p
            p = p.next
            pre.next = None
        def reverse(p):
            newHead = ListNode('')
            while p:
                q = p
                p = p.next
                q.next = newHead.next
                newHead.next = q
            return newHead.next
        p = reverse(p)
        q = head
        while q.next:
            if q.val != p.val:
                return False
            q = q.next
            p = p.next
        return True




head = ListNode.create_list([1,2, 1,2])
print(Solution().isPalindrome(head))
        return c

    def sortedListToBST(self, head: ListNode) -> TreeNode:
        '''
        根据中序遍历的顺序去构造二叉树,正好中序遍历的顺序就是链表的顺序,中序遍历是递归的
        '''
        size = self.findSize(head)

        def convert(l, r):
            nonlocal head
            if l > r:
                return None
            mid = (l + r) // 2

            # 中序遍历左子树
            left = convert(l, mid - 1)

            # 构造左子树之后构造当前节点
            node = TreeNode(head.val)
            node.left = left

            head = head.next

            node.right = convert(mid + 1, r)
            return node

        return convert(0, size - 1)


head = ListNode.create_list([-10, -3, 0, 5, 9])
Solution().sortedListToBST(head).printTree()