Ejemplo n.º 1
0
 def equeue(self, value):
     new = Node(value)
     if self._head is None:
         self._head = new
         self._tail = new
     else:
         self._tail.next = new
         self._tail = self._tail.next
     self._count += 1
Ejemplo n.º 2
0
def partation(alist, target):
    dummy_left = left_head = Node()
    dummy_right = right_head = Node()
    head = alist.head.next

    while head is not None:
        if target.value > head.value:
            left_head.next = head
            left_head = left_head.next
            head = head.next
        else:
            right_head.next = head
            right_head = right_head.next
            head = head.next
    left_head.next = target
    target.next = dummy_right.next
    right_head.next = None
    return dummy_left
Ejemplo n.º 3
0
def LinkListtoBST(head):
    if head is None:
        return None

    dummy = Node(0)
    dummy.next = head

    fast = dummy
    slow = dummy
    slow_tail = dummy

    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow_tail = slow
        slow = slow.next

    slow_tail.next = None
    node = Nd(slow.value)
    node._left = LinkListtoBST(dummy.next)
    node._right = LinkListtoBST(slow.next)
    return node
Ejemplo n.º 4
0
def merge_list(node_a, node_b):
    dummy = cur = Node()
    while node_a != None and node_b != None:
        if node_a.value <= node_b.value:
            cur.next = node_a
            node_a = node_a.next
        else:
            cur.next = node_b
            node_b = node_b.next
        cur = cur.next

    cur.next = node_a or node_b
    return dummy.next
Ejemplo n.º 5
0
def merge(alist):
    dummy = Node(0)
    cur = alist.head.next

    while cur is not None:
        pre = dummy
        while pre.next is not None and pre.next.value < cur.value:
            pre = pre.next
        temp = cur.next
        cur.next = pre.next
        pre.next = cur
        cur = temp
    return dummy.next
Ejemplo n.º 6
0
def palindrome(alist):
    head = alist.head
    if head == None:
        return head

    #1.find mid point
    fast = head.next
    slow = head
    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next

    #2.split into two linklist
    head2 = Node()
    if fast == None:
        head2.next = slow.next
        slow.next = None
    else:
        head2.next = slow.next.next
        slow.next = None

    #3.reverse second linklist
    pre = None
    cur = head2.next
    while cur:
        temp = cur.next
        cur.next = pre
        pre = cur
        cur = temp

    # judging if it is palindrome
    while pre.next is not None:
        if pre.value != head.next.value:
            return False
        else:
            pre = pre.next
            head = head.next

    return True
Ejemplo n.º 7
0
def merge(left, right):
    dummy = head = Node(0)
    while left and right:
        if left.value < right.value:
            head.next = left
            left = left.next
        else:
            head.next = right
            right = right.next
        head = head.next
    if left != None:
        head.next = left
    if right != None:
        head.next =right
    return dummy.next
Ejemplo n.º 8
0
def merge_list2(list_a, list_b):
    a = list_a.head.next
    b = list_b.head.next
    result = c = Node()
    while a != None and b != None:
        if a.value <= b.value:
            c.next = a
            a = a.next
        else:
            c.next = b
            b = b.next
        c = c.next
    c.next = a or b
    ll = LinkList()
    ll.head.next = result.next
    return ll
Ejemplo n.º 9
0
    slow_tail = dummy

    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow_tail = slow
        slow = slow.next

    slow_tail.next = None
    node = Nd(slow.value)
    node._left = LinkListtoBST(dummy.next)
    node._right = LinkListtoBST(slow.next)
    return node


if __name__ == '__main__':
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    node5.next = node6

    bst = BinarySearchTree()
    result = LinkListtoBST(node1)
    bst._root = result
    bst.print_inorder()
Ejemplo n.º 10
0
    if node.next is None:
        return node
    head = reverse_recursion(node.next)
    node.next.next = node
    node.next = None
    return head


if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(1)
    ll.add_last(2)
    ll.add_last(3)
    ll.add_last(4)
    ll.print_list()
    print()
    reverse_l(ll)
    ll.print_list()
    print()
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node1.next = node2
    node2.next = node3
    node3.next = node4
    result = reverse_recursion(node1)
    l2 = LinkList()
    l2.head.next = result
    l2.print_list()
Ejemplo n.º 11
0
if __name__ == '__main__':
    ll_a = LinkList()
    ll_a.add_last(1)
    ll_a.add_last(3)
    ll_a.add_last(5)
    ll_a.add_last(7)
    ll_b = LinkList()
    ll_b.add_last(2)
    ll_b.add_last(4)
    ll_b.add_last(5)
    ll_b.add_last(8)
    ll_b.add_last(10)
    ll_b.add_last(12)

    node1 = Node(1)
    node2 = Node(3)
    ndoe3 = Node(5)
    node1.next = node2
    node2.next = ndoe3

    node_a = Node(2)
    node_b = Node(4)
    node_c = Node(5)
    node_d = Node(8)
    node_a.next = node_b
    node_b.next = node_c
    node_c.next = node_d
    #result = merge_list(node1, node_a)

    #ll = LinkList()
Ejemplo n.º 12
0
    dummy_right = right_head = Node()
    head = alist.head.next

    while head is not None:
        if target.value > head.value:
            left_head.next = head
            left_head = left_head.next
            head = head.next
        else:
            right_head.next = head
            right_head = right_head.next
            head = head.next
    left_head.next = target
    target.next = dummy_right.next
    right_head.next = None
    return dummy_left

if __name__ == '__main__':
    ll = LinkList()
    ll.add_last(8)
    ll.add_last(3)
    ll.add_last(7)
    ll.add_last(2)
    ll.add_last(1)
    ll.add_last(9)
    t = Node(5)
    result = partation(ll, t)
    l2 = LinkList()
    l2.head = result
    l2.print_list()
Ejemplo n.º 13
0

def find_cycle(llist):
    return find_cycle_helper(llist.head)


def find_cycle_helper(head):
    if head is None:
        return False

    fast = head
    slow = head

    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next
        if slow == fast:
            return True
    return False


if __name__ == '__main__':
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node1.next = node2
    node2.next = node3
    node3.next = node1

    result = find_cycle_helper(node1)
    print(result)
Ejemplo n.º 14
0

#second method,


def find_same2(a, b):
    head_a = a
    head_b = b
    while a != b:
        a = a.next if a else head_b
        b = b.next if b else head_a
    return a.value


if __name__ == '__main__':
    a1 = Node(1)
    a2 = Node(3)
    a3 = Node(5)
    c1 = Node(9)
    c2 = Node(10)
    c3 = Node(11)
    b1 = Node(2)
    b2 = Node(4)
    b3 = Node(6)
    b4 = Node(8)

    a1.next = a2
    a2.next = a3
    a3.next = c1
    c1.next = c2
    c2.next = c3