Ejemplo n.º 1
0
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1 and not l2:
            return None
        l1_node, l2_node = l1, l2
        l = dummy_new_head = ListNode(1)
        while l1_node and l2_node:
            if l1_node.val <= l2_node.val:
                l.next = ListNode(l1_node.val)
                l1_node = l1_node.next
            else:
                l.next = ListNode(l2_node.val)
                l2_node = l2_node.next
            l = l.next
        l.next = l1_node or l2_node
        return dummy_new_head.next


if __name__ == "__main__":
    ListNode.print_linked_list(ListNode.build_linked_list([1, 2, 3]))
    #print_ListNode(Solution().mergeTwoLists(None, None))
    #print_ListNode(Solution().mergeTwoLists(None, ListNode(1)))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([2]), build_ListNode([1])))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([1]), build_ListNode([2])))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([1,2,3]), build_ListNode([2, 5,7])))
    ListNode.print_linked_list(Solution().mergeTwoLists(
        ListNode.build_linked_list([1, 2, 3]),
        ListNode.build_linked_list([2, 5, 7])))
Ejemplo n.º 2
0
        slow.next = None
        prev.next = None
        # reverse the list from the mid
        while current:
            next = current.next
            current.next = prev
            prev, current = current, next
        # interleave the two list
        current1, current2 = head, prev
        while current1 and current2:
            current1_next, current2_next = current1.next, current2.next
            current1.next, current2.next = current2, current1_next
            current1, current2 = current1_next, current2_next

if __name__ == '__main__':
    l1 = ListNode.build_linked_list([1,2,3])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1,2])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1,2,3,4,5,6])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1,2,3,4,5,6,7])
Ejemplo n.º 3
0
        # count the length and get last_node
        length = 0
        current = head
        while current:
            length += 1
            if current.next is None:
                last_node = current
            current = current.next
        k = k % length
        if k == 0:
            return head
        idx = 0
        current = head
        while current:
            if idx == length - k - 1:
                target_prev, target_current = current, current.next
            current = current.next
            idx += 1
        new_head = target_current
        last_node.next = head
        target_prev.next = None
        return new_head

if __name__ == '__main__':
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 2))
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 3))
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 4))
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 5))
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2,3,4,5]), 5))
    ListNode.print_linked_list(Solution().rotateRight(ListNode.build_linked_list([1,2]), 2))
Ejemplo n.º 4
0
#
#   This is very tricky, see proof in Gossip.md
#

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                slow = head
                while fast != slow:
                    fast = fast.next
                    slow = slow.next
                return slow
        return None


if __name__ == '__main__':
    head = ListNode.build_linked_list([1,2,3,4,5])
    head.next.next.next.next.next = head.next.next
    print Solution().detectCycle(head).val
    print Solution().detectCycle(None)


__author__ = 'clp'

from utils import ListNode

class Solution(object):

    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        while head is not None and head.val == val:
            head = head.next
        if not head:
            return None
        current_node = head
        while current_node.next:
            if current_node.next.val == val:
                current_node.next = current_node.next.next
            else:
                current_node = current_node.next
        return head

if __name__ == '__main__':
    ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1,2,3,4,5,6]),  6))
    ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1,1,3,4,56]),  1))
    ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([2,1,3,4,4,4,4]),  4))
    ListNode.print_linked_list(Solution().removeElements(ListNode.build_linked_list([1]),  1))
Ejemplo n.º 6
0
        current = head
        while current:
            if current.val < x:
                if not f_head:
                    f_head = current
                    f_end = f_head
                else:
                    f_end.next = current
                    f_end = f_end.next
            else:
                if not b_head:
                    b_head = current
                    b_end = b_head
                else:
                    b_end.next = current
                    b_end = b_end.next
            current = current.next
        if f_head:
            f_end.next = b_head
        if b_end:
            b_end.next = None
        return f_head if f_head else b_head

if __name__ == '__main__':
    ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 3))
    ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 6))
    ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 1))
    ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1, 4, 3, 2, 5, 2]), 2))
    ListNode.print_linked_list(Solution().partition(ListNode.build_linked_list([1]), 2))
    ListNode.print_linked_list(Solution().partition(None, 2))
        :rtype: ListNode
        """
        if not head:
            return None
        hash_counter = {}
        current = head
        while current:
            k = current.val
            hash_counter[k] = hash_counter.get(k, 0) + 1
            current = current.next
        new_head = None
        prev, current = None, head
        while current:
            if hash_counter[current.val] >= 2:
                if prev:
                    prev.next = current.next
                prev, current = prev, current.next
            else:
                if not new_head:
                    new_head = current
                prev, current = current, current.next
        return new_head


if __name__ == '__main__':
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,1,2,3,1,1,2])))
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,1,2,3,1,1])))
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1,2,3,3,4,4,5])))
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1, 5])))
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([1])))
    ListNode.print_linked_list(Solution().deleteDuplicates(ListNode.build_linked_list([])))
Ejemplo n.º 8
0
        while current:
            k = current.val
            hash_counter[k] = hash_counter.get(k, 0) + 1
            current = current.next
        new_head = None
        prev, current = None, head
        while current:
            if hash_counter[current.val] >= 2:
                if prev:
                    prev.next = current.next
                prev, current = prev, current.next
            else:
                if not new_head:
                    new_head = current
                prev, current = current, current.next
        return new_head


if __name__ == '__main__':
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([1, 1, 2, 3, 1, 1, 2])))
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([1, 1, 2, 3, 1, 1])))
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([1, 2, 3, 3, 4, 4, 5])))
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([1, 5])))
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([1])))
    ListNode.print_linked_list(Solution().deleteDuplicates(
        ListNode.build_linked_list([])))
Ejemplo n.º 9
0
        prev.next = None
        # reverse the list from the mid
        while current:
            next = current.next
            current.next = prev
            prev, current = current, next
        # interleave the two list
        current1, current2 = head, prev
        while current1 and current2:
            current1_next, current2_next = current1.next, current2.next
            current1.next, current2.next = current2, current1_next
            current1, current2 = current1_next, current2_next


if __name__ == '__main__':
    l1 = ListNode.build_linked_list([1, 2, 3])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1, 2])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1, 2, 3, 4, 5, 6])
    Solution().reorderList(l1)
    ListNode.print_linked_list(l1)
    l1 = ListNode.build_linked_list([1, 2, 3, 4, 5, 6, 7])
Ejemplo n.º 10
0
        while current:
            if c == m:
                r_head_prev, r_head = prev, current
                prev, current = current, current.next
            elif m < c < n:
                next = current.next
                current.next = prev
                prev, current = current, next
            elif c == n:
                r_tail, r_tail_next = current, current.next
                current.next = prev
                prev, current = current, r_tail_next
            else:
                prev, current = current, current.next
            c += 1
        if r_head_prev:
            r_head_prev.next = r_tail
        r_head.next = r_tail_next

        return head if m > 1 else r_tail


if __name__ == '__main__':
    ListNode.print_linked_list(Solution().reverseBetween(
        ListNode.build_linked_list([1, 2, 3, 4, 5]), 2, 4))
    ListNode.print_linked_list(Solution().reverseBetween(
        ListNode.build_linked_list([1, 2, 3, 4, 5]), 1, 4))
    ListNode.print_linked_list(Solution().reverseBetween(
        ListNode.build_linked_list([1, 2, 3, 4, 5]), 1, 5))
    ListNode.print_linked_list(Solution().reverseBetween(
        ListNode.build_linked_list([1, 2, 3, 4, 5]), 2, 2))
            return node

        lA, lB = link_len(headA), link_len(headB)
        if lA > lB:
            hA, hB = link_index(headA, lA - lB), headB
        elif lA < lB:
            hA, hB = headA, link_index(headB, lB - lA)
        else:
            hA, hB = headA, headB

        nA, nB = hA, hB
        while nA and nB:
            if nA == nB:
                return nA
            nA = nA.next
            nB = nB.next
        return None


if __name__ == '__main__':
    headC = ListNode.build_linked_list([4,5,6])
    headA = ListNode(1)
    headA.next = ListNode(2)
    headA.next.next = headC
    headB = headC

    print Solution().getIntersectionNode(headA, headB)
    print Solution().getIntersectionNode(None, None)
    print Solution().getIntersectionNode(ListNode.build_linked_list([1,3,5,7,9,11,13,15,17,19,21]),
                                         ListNode.build_linked_list([2]))
Ejemplo n.º 12
0
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        if not head:
            return None

        global new_head

        def reverse_list(node):
            global new_head
            if node.next is None:
                new_head = node
                return node
            next_node = reverse_list(node.next)
            next_node.next = node
            return node

        reverse_list(head)
        head.next = None
        return new_head


if __name__ == '__main__':
    l = ListNode.build_linked_list([])
    l = ListNode.build_linked_list([1])
    # l = ListNode.build_linked_list([1, 2, 3, 4, 5])
    ListNode.print_linked_list(Solution().reverseList(l))
Ejemplo n.º 13
0
        lA, lB = link_len(headA), link_len(headB)
        if lA > lB:
            hA, hB = link_index(headA, lA - lB), headB
        elif lA < lB:
            hA, hB = headA, link_index(headB, lB - lA)
        else:
            hA, hB = headA, headB

        nA, nB = hA, hB
        while nA and nB:
            if nA == nB:
                return nA
            nA = nA.next
            nB = nB.next
        return None


if __name__ == '__main__':
    headC = ListNode.build_linked_list([4, 5, 6])
    headA = ListNode(1)
    headA.next = ListNode(2)
    headA.next.next = headC
    headB = headC

    print Solution().getIntersectionNode(headA, headB)
    print Solution().getIntersectionNode(None, None)
    print Solution().getIntersectionNode(
        ListNode.build_linked_list([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]),
        ListNode.build_linked_list([2]))
Ejemplo n.º 14
0

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        while head is not None and head.val == val:
            head = head.next
        if not head:
            return None
        current_node = head
        while current_node.next:
            if current_node.next.val == val:
                current_node.next = current_node.next.next
            else:
                current_node = current_node.next
        return head


if __name__ == '__main__':
    ListNode.print_linked_list(Solution().removeElements(
        ListNode.build_linked_list([1, 2, 3, 4, 5, 6]), 6))
    ListNode.print_linked_list(Solution().removeElements(
        ListNode.build_linked_list([1, 1, 3, 4, 56]), 1))
    ListNode.print_linked_list(Solution().removeElements(
        ListNode.build_linked_list([2, 1, 3, 4, 4, 4, 4]), 4))
    ListNode.print_linked_list(Solution().removeElements(
        ListNode.build_linked_list([1]), 1))
Ejemplo n.º 15
0
            return head
        elif m == n:
            return head
        c = 1
        prev, current = None, head
        while current:
            if c == m:
                r_head_prev, r_head = prev, current
                prev, current = current, current.next
            elif m < c < n:
                next = current.next
                current.next = prev
                prev, current = current, next
            elif c == n:
                r_tail, r_tail_next = current, current.next
                current.next = prev
                prev, current = current, r_tail_next
            else:
                prev, current = current, current.next
            c += 1
        if r_head_prev:
            r_head_prev.next = r_tail
        r_head.next = r_tail_next

        return head if m > 1 else r_tail

if __name__ == '__main__':
    ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 2, 4))
    ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 1, 4))
    ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 1, 5))
    ListNode.print_linked_list(Solution().reverseBetween(ListNode.build_linked_list([1,2,3,4,5]), 2, 2))
Ejemplo n.º 16
0
                sorted_prev, sorted_current = sorted_head, sorted_head.next
                while sorted_current:
                    if sorted_current.val > current.val:
                        sorted_prev.next = current
                        current.next = sorted_current
                        break
                    sorted_prev, sorted_current = sorted_current, sorted_current.next
                if current.val >= sorted_end.val:
                    current.next = None
                    sorted_end.next = current
                    sorted_end = current

            current = current_next

        return sorted_head


if __name__ == '__main__':
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([5, 1, 4, 3, 2])))
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([1])))
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([])))
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([8, 7])))
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([8, 7, 1])))
    ListNode.print_linked_list(Solution().insertionSortList(
        ListNode.build_linked_list([1])))
Ejemplo n.º 17
0
#
#   This is very tricky, see proof in Gossip.md
#


class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                slow = head
                while fast != slow:
                    fast = fast.next
                    slow = slow.next
                return slow
        return None


if __name__ == '__main__':
    head = ListNode.build_linked_list([1, 2, 3, 4, 5])
    head.next.next.next.next.next = head.next.next
    print Solution().detectCycle(head).val
    print Solution().detectCycle(None)
Ejemplo n.º 18
0
        :rtype: ListNode
        """
        if not l1 and not l2:
            return None
        l1_node, l2_node = l1, l2
        l = dummy_new_head = ListNode(1)
        while l1_node and l2_node:
            if l1_node.val <= l2_node.val:
                l.next = ListNode(l1_node.val)
                l1_node = l1_node.next
            else:
                l.next = ListNode(l2_node.val)
                l2_node = l2_node.next
            l = l.next
        l.next = l1_node or l2_node
        return dummy_new_head.next

if __name__ == "__main__":
    ListNode.print_linked_list(ListNode.build_linked_list([1,2,3]))
    #print_ListNode(Solution().mergeTwoLists(None, None))
    #print_ListNode(Solution().mergeTwoLists(None, ListNode(1)))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([2]), build_ListNode([1])))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([1]), build_ListNode([2])))
    #print_ListNode(Solution().mergeTwoLists(build_ListNode([1,2,3]), build_ListNode([2, 5,7])))
    ListNode.print_linked_list(
        Solution().mergeTwoLists(
            ListNode.build_linked_list([1, 2, 3]),
            ListNode.build_linked_list([2, 5, 7])
        )
    )
from utils import TreeNode, ListNode

class Solution(object):
    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """
        if head is None:
            return None
        fast, prev, slow = head, None, head
        while fast and fast.next:
            fast = fast.next.next
            prev, slow = slow, slow.next
        root = TreeNode(slow.val)
        if prev:
            prev.next = None
        else:
            return root
        root.left = self.sortedListToBST(head)
        root.right = self.sortedListToBST(slow.next)
        return root


if __name__ == '__main__':
    TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2])))
    TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1])))
    TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2,3,4,5,6])))
    TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([1,2,3,4,5])))
    TreeNode.print_in_level_order(Solution().sortedListToBST(ListNode.build_linked_list([])))
Ejemplo n.º 20
0
__author__ = 'clp'

from utils import ListNode


class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        if node is None:
            return
        current_node = node
        while current_node.next:
            current_node.val = current_node.next.val
            if current_node.next.next:
                current_node = current_node.next
            else:
                current_node.next = None


if __name__ == "__main__":
    l1 = ListNode.build_linked_list([1, 2, 3, 4])
    l1_node_3 = l1.next.next
    Solution().deleteNode(l1_node_3)
    ListNode.print_linked_list(l1)
Ejemplo n.º 21
0
        head.next = None
        while current:
            current_next = current.next
            if current.val < sorted_head.val:
                current.next = sorted_head
                sorted_head = current
            else:
                sorted_prev, sorted_current = sorted_head, sorted_head.next
                while sorted_current:
                    if sorted_current.val > current.val:
                        sorted_prev.next = current
                        current.next = sorted_current
                        break
                    sorted_prev, sorted_current = sorted_current, sorted_current.next
                if current.val >= sorted_end.val:
                    current.next = None
                    sorted_end.next = current
                    sorted_end = current

            current = current_next

        return sorted_head

if __name__ == '__main__':
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([5,1,4,3,2])))
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([1])))
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([])))
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([8,7])))
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([8,7,1])))
    ListNode.print_linked_list(Solution().insertionSortList(ListNode.build_linked_list([1])))