Ejemplo n.º 1
0
    def get(self, key):
        node = self.hash.get(key)

        if node:
            self._move_to_front(node)
            print_ll(self.first_page)
            return node.data

        if self.first_page:
            print_ll(self.first_page)
        return None
def get_max_list(a, b):
    result = None
    pre1 = curr1 = a
    pre2 = curr2 = b

    while curr1 is not None or curr2 is not None:
        sum1 = 0
        sum2 = 0

        # iterating till and equal node is found
        while curr1 is not None and curr2 is not None and curr1.data != curr2.data:

            if curr1.data < curr2.data:
                sum1 += curr1.data
                curr1 = curr1.nxt
            else:
                sum2 += curr2.data
                curr2 = curr2.nxt

        # if one of them becomes none in the process
        if curr1 is None:
            while curr2 is not None:
                sum2 += curr2.data
                curr2 = curr2.nxt

        if curr2 is None:
            while curr1 is not None:
                sum1 += curr1.data
                curr1 = curr1.nxt

        # initialization of result for the first time based on sum
        if pre1 == a and pre2 == b:
            result = pre1 if sum1 > sum2 else pre2

        else:
            # further decisions based on sum
            if sum1 > sum2:
                pre2.nxt = pre1.nxt
            else:
                pre1.nxt = pre2.nxt

        pre1 = curr1
        pre2 = curr2

        if curr1 is not None:
            curr1 = curr1.nxt

        if curr2 is not None:
            curr2 = curr2.nxt

    if result:
        print_ll(result)
Ejemplo n.º 3
0
    def put(self, key):
        if key in self.hash:
            node = self.hash[key]
            self._move_to_front(node)

        else:
            if self.node_count == self.max_count:
                self._remove_last_node()

            node = DNode(key)
            self.hash[key] = node
            self._add_to_front(node)
            self.node_count += 1

        print_ll(self.first_page)
Ejemplo n.º 4
0
        if new_head:
            new_tail.nxt = eqll_h
        else:
            new_head = eqll_h
            new_tail = eqll_t

    if gtll_h:
        if new_head:
            new_tail.nxt = gtll_h
        else:
            new_head = gtll_h

    return new_head


if __name__ == '__main__':
    ll = create_linked_list([1, 4, 3, 2, 5, 2, 3])
    res = partition_ll(ll, 3)
    print_ll(res)

    print ''

    ll = create_linked_list([1, 4, 2, 10])
    res = partition_ll(ll, 3)
    print_ll(res)

    print ''

    ll = create_linked_list([10, 4, 20, 10, 3])
    res = partition_ll(ll, 3)
    print_ll(res)
            if head == h:
                prev, h, nxt, t = move_node_to_end(prev, h, nxt, True, t)
                head = h
            else:
                prev, h, nxt, t = move_node_to_end(prev, h, nxt, False, t)
        else:
            prev = h
            h = nxt
            nxt = nxt.nxt

    return head


if __name__ == '__main__':
    _h = create_linked_list([17, 15, 8, 12, 10, 5, 4, 1, 7, 6])
    print_ll(segregate_odd_and_even(_h))
"""
better approach
"""


def add_to_ll(head, tail, node):
    if head:
        tail.nxt = node
        return head, node

    return node, node


def segregate_odd_and_even_another_app(head):
    odd_h = odd_t = even_h = even_t = None
Ejemplo n.º 6
0
    add_to_list(head)

    nxt = head.nxt
    flatten_depth_wise(head.down)
    flatten_depth_wise(nxt)


if __name__ == '__main__':
    l1 = create_linked_list([1, 2, 3, 4], node=Node)
    n2 = l1.nxt
    n2.down = create_linked_list([7, 8, 10, 12], node=Node)

    l2 = n2.down
    l2.down = Node(9)
    l2.down.down = Node(14)
    l2.down.down.down = Node(15)
    l2.down.down.down.nxt = Node(23)
    l2.down.down.down.nxt.down = Node(24)

    l2n2 = l2.nxt
    l2n2.down = Node(16)
    l2n2.down.down = create_linked_list([17, 18, 19, 20], node=Node)
    l2n2.down.down.nxt.nxt.nxt.down = Node(21)

    l2.nxt.nxt.down = Node(11)

    flatten_depth_wise(l1)
    print ''
    print_ll(HEAD)
Ejemplo n.º 7
0
"""
merge two sorted linked lists
"""
from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll


def sorted_merge(a, b):
    if a is None:
        return b

    if b is None:
        return a

    if a.data > b.data:
        result = b
        result.nxt = sorted_merge(a, b.nxt)

    else:
        result = a
        result.nxt = sorted_merge(b, a.nxt)

    return result


if __name__ == '__main__':
    l1 = create_linked_list([1, 3, 5, 7, 9])
    l2 = create_linked_list([2, 4, 6, 8, 10])

    r = sorted_merge(l1, l2)
    print_ll(r)
        if clone_node.nxt:
            clone_node.nxt = clone_node.nxt.nxt

        node = node.nxt

    return cl_start


if __name__ == '__main__':
    # with extra space
    ll1 = create_linked_list([1, 2, 3, 4], node=Node)
    ll1.other = ll1.nxt.nxt
    ll1.nxt.other = ll1.nxt.nxt.nxt
    ll1.nxt.nxt.other = ll1
    ll1.nxt.nxt.nxt.other = ll1.nxt
    print_ll(ll1)
    ll2 = clone_without_extra_space(ll1)
    print_ll(ll2)
    print ll2.other
    print ll2.nxt.other
    print ll2.nxt.nxt.other
    print ll2.nxt.nxt.nxt.other

    print ''

    # without extra space
    ll1 = create_linked_list([1, 2, 3, 4], node=Node)
    ll1.other = ll1.nxt.nxt
    ll1.nxt.other = ll1.nxt.nxt.nxt
    ll1.nxt.nxt.other = ll1
    ll1.nxt.nxt.nxt.other = ll1.nxt
Ejemplo n.º 9
0
"""
http://www.geeksforgeeks.org/rotate-a-linked-list/
"""
from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll
from G4G.Problems.linked_list.segregate_odd_and_even_in_ll import get_tail_ll


def rotate_ll(head, k):
    # converting to circular ll temporarily
    tail = get_tail_ll(head)
    tail.nxt = head

    while k > 0:
        tail = head
        head = head.nxt
        k -= 1

    tail.nxt = None
    return head


if __name__ == '__main__':
    h = create_linked_list([1, 2, 3, 4, 5, 6])
    n = rotate_ll(h, 9)
    print_ll(n)
Ejemplo n.º 10
0
    while b is not None and b.nxt is not None:
        a.nxt = a.nxt.nxt
        b.nxt = b.nxt.nxt

        a = a.nxt
        b = b.nxt

    a.nxt = None

    return h1, h2


if __name__ == '__main__':
    h = create_linked_list([0, 1, 0, 1, 0, 1])
    _h1, _h2 = alternate_split(h)
    print_ll(_h1)
    print_ll(_h2)

    print ''

    h = create_linked_list([0, 1, 0, 1, 0])
    _h1, _h2 = alternate_split(h)
    print_ll(_h1)
    print_ll(_h2)

    print ''

    h = create_linked_list([0, 1])
    _h1, _h2 = alternate_split(h)
    print_ll(_h1)
    print_ll(_h2)
Ejemplo n.º 11
0
    node.nxt = prev
    return prev


def reverse2(node, prev):
    if node is None:
        return prev
    else:
        temp = reverse2(node.nxt, node)
        node.nxt = prev
        return temp


if __name__ == '__main__':
    start = create_linked_list([1, 2, 3, 4, 5, 3, 2, 6, 7, 8, 9, 4, 7])
    print_ll(start)

    new_head = reverse2(start, None)

    print_ll(new_head)
"""
Iterative reverse
"""


def iterative_reverse(head):
    prev = head
    curr = head.nxt
    prev.nxt = None

    while curr:
Ejemplo n.º 12
0
"""
Delete a node from an ll given access to only that node
"""
from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll, get_node


def delete_node(node):
    if node.nxt:
        next_node = node.nxt
        node.data = next_node.data
        node.nxt = next_node.nxt
        next_node.nxt = None
        del next_node


start = create_linked_list([1, 2, 3, 4, 5, 3, 2, 6, 7, 8, 9, 4, 7])
print_ll(start)
node = get_node(start, 3)
print node.data

delete_node(node)
print_ll(start)
Ejemplo n.º 13
0
    t.nxt = head
    return h


def add_lls(h1, h2):
    c1 = count_nodes(h1)
    c2 = count_nodes(h2)

    ll = h1 if c1 >= c2 else h2
    sl = h1 if ll == h2 else h2

    sl = append_zeros(sl, abs(c1 - c2))

    carry = add_eq_len_lls(ll, sl)

    if carry > 0:
        head = Node(carry)
        head.nxt = h1
        return head

    return h1


if __name__ == '__main__':
    l1 = create_linked_list([5, 6, 3])
    l2 = create_linked_list([8, 4, 2])

    _sum_list = add_lls(l1, l2)

    print_ll(_sum_list)
Ejemplo n.º 14
0
from sys import maxint
# 1 2 3
from G4G.Problems.dp.min_matrix_cost_path_to_mn import print_matrix
from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll


def reverse(head, prev):
    if head is None:
        return prev, head

    # 9, 8 - 8, 7
    a, b = reverse(head.nxt, head)

    if b is None:
        return head, head

    b.nxt = head
    return a, head


if __name__ == '__main__':
    h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8, 9])
    rl, rt = reverse(h, None)
    rt.nxt = None
    print_ll(rl)
Ejemplo n.º 15
0
            n2.nxt = nxt1

        else:
            # somewhere in between
            pre1.nxt = n2
            n2.nxt = nxt1
            pre2.nxt = n1
            n1.nxt = nxt2

    return head


if __name__ == '__main__':
    h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8])
    h1 = swap_kth_beg_and_end(h, 1)
    print_ll(h1)

    print ''

    h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8])
    h1 = swap_kth_beg_and_end(h, 2)
    print_ll(h1)

    print ''

    h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8])
    h1 = swap_kth_beg_and_end(h, 3)
    print_ll(h1)

    print ''

def sort_abs_val_sorted_ll(head):
    curr = head.nxt
    prev = head

    while curr is not None:

        if curr.data < prev.data:

            prev.nxt = curr.nxt

            curr.nxt = head
            head = curr

            curr = prev

        else:
            prev = curr

        curr = curr.nxt

    return head


if __name__ == '__main__':
    h = create_linked_list([0, 1, -2, 3, 4, 5, -5])
    sh = sort_abs_val_sorted_ll(h)

    print_ll(sh)
Ejemplo n.º 17
0
    while head1 is not None and head2 is not None:
        if head1.data <= head2.data:
            node = head1
            head1 = head1.nxt
            head = add_to_list(node, head)

        else:
            node = head2
            head2 = head2.nxt
            head = add_to_list(node, head)

    while head1 is not None:
        node = head1
        head1 = head1.nxt
        head = add_to_list(node, head)

    while head2 is not None:
        node = head2
        head2 = head2.nxt
        head = add_to_list(node, head)

    return head


if __name__ == '__main__':
    h1 = create_linked_list([5, 10, 15, 40])
    h2 = create_linked_list([2, 3, 20])

    h = merge_sorted_lls(h1, h2)
    print_ll(h)
Ejemplo n.º 18
0
    if l1.power >= l2.power:
        lp = l1
        sp = l2

    else:
        lp = l2
        sp = l1

    res = lp

    while lp.power != sp.power:
        lp = lp.nxt

    while lp is not None:
        lp.data += sp.data
        lp = lp.nxt
        sp = sp.nxt

    return res


if __name__ == '__main__':
    l1 = Node(5, 2)
    l1.nxt = Node(4, 1)
    l1.nxt.nxt = Node(2, 0)

    l2 = Node(5, 1)
    l2.nxt = Node(5, 0)

    print_ll(add_poly(l1, l2))