Ejemplo n.º 1
0
    def head(self):
        """
        Initialize the linked list.
        """
        head = Node(5)
        linked_list = LinkedList(head)

        linked_list.append(Node(4))
        linked_list.append(Node(3))
        linked_list.append(Node(2))
        linked_list.append(Node(1))

        return head
def sum_lists(ll_a, ll_b):
    n1, n2 = ll_a.head, ll_b.head
    ll = LinkedList()
    carry = 0
    while n1 or n2:
        result = carry
        if n1:
            result += n1.value
            n1 = n1.next
        if n2:
            result += n2.value
            n2 = n2.next

        ll.append(result % 10)
        carry = result // 10

    if carry:
        ll.append(carry)

    return ll
Ejemplo n.º 3
0
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        if not l1:
            return l2

        if not l2:
            return l1

        head = None
        if l1.val < l2.val:
            head = l1
        else:
            head = l2

        target = LinkedList()

        while True:
            if not l1 and not l2:
                break

            if not l1:
                target.append(l2.val)
                l2 = l2.next
                continue

            if not l2:
                target.append(l1.val)
                l1 = l1.next
                continue

            if l1.val < l2.val:
                target.append(l1.val)
                l1 = l1.next
            else:
                target.append(l2.val)
                l2 = l2.next

        return target.head
Ejemplo n.º 4
0
    def test_sum_lists_reversed(self):
        ll_1 = LinkedList(Node(9))
        ll_2 = LinkedList(Node(7))

        ll_1.append(Node(1))
        ll_1.append(Node(3))
        ll_1.append(Node(2))
        ll_1.append(Node(1))

        ll_2.append(Node(8))
        ll_2.append(Node(7))

        sum_head = sum_lists_reverse(ll_1.head, ll_2.head)

        assert sum_head.data == 9
        assert sum_head.next.data == 2
        assert sum_head.next.next.data == 1
        assert sum_head.next.next.next.data == 0
        assert sum_head.next.next.next.next.data == 8
    diff = longer.length - shorter.length

    short, long = shorter.head, longer.head

    for i in range(diff):
        long = long.next

    while short is not long:
        short = short.next
        long = long.next

    return long.value


ll_1 = LinkedList()
ll_2 = LinkedList()
ll_1.append_multiple([2, 8, 4, 9, 2])
ll_2.append_multiple([3, 4, 1])
node = Node(5)
ll_1.append(node)
ll_2.append(node)
node = Node(1)
ll_1.append(node)
ll_2.append(node)

ll_1.display()
ll_2.display()

result = intersection(ll_1, ll_2)
print(result)
Ejemplo n.º 6
0
from utils import LinkedList


def search(ll_list, key):
    tmp = ll_list.head
    while tmp is not None:
        if tmp.data == key:
            return True
        tmp = tmp.next
    return False


if __name__ == '__main__':
    linked_list = LinkedList()
    linked_list.append(3)
    linked_list.push(4)
    linked_list.push(1)
    linked_list.append(5)
    print(search(linked_list, 43))
from utils import LinkedList


def delete_middle_node(node):
    node.value = node.next.value
    node.next = node.next.next


ll = LinkedList()
ll.append_multiple([1, 2, 3, 4])
middle_node = ll.append(5)
ll.append_multiple([6, 7, 8, 9])

ll.display()
delete_middle_node(middle_node)
ll.display()
from utils import LinkedList


def partition(ll, x):
    current = ll.tail = ll.head

    while current:
        nextNode = current.next
        current.next = None
        if current.value < x:
            current.next = ll.head
            ll.head = current
        else:
            ll.tail.next = current
            ll.tail = current
        current = nextNode

    # Error check in case all nodes are less than x
    if ll.tail.next is not None:
        ll.tail.next = None
    return ll


ll = LinkedList()
n = ll.append(55)
ll.append_multiple([82, 95, 20, 47, 37, 11, 42, 91, 6, 74])
ll.display()
print('Patition created at {}'.format(n.value))
ll = partition(ll, n.value)
ll.display()
Ejemplo n.º 9
0
    def head(self):
        """
        Initialize linked list.
        """
        head = Node(1)
        linked_list = LinkedList(head)

        linked_list.append(Node('hello'))
        linked_list.append(Node('why'))
        linked_list.append(Node('34'))
        linked_list.append(Node('0293148'))
        linked_list.append(Node(2034820))
        linked_list.append(Node(True))
        linked_list.append(Node(False))

        return head