def sum_lists(list1, list2):
    size_list1 = list1.size()
    size_list2 = list2.size()
    diff = size_list1 - size_list2

    if diff > 0:
        fill(list2, diff)

    elif diff < 0:
        fill(list1, diff)

    list1.insertToHead(0)
    list2.insertToHead(0)

    result = LinkedList()
    result.insert(0)

    node1 = list1.head
    node2 = list2.head
    node_res = result.head

    cal_sum_list(node1, node2, node_res)

    if result.head.data == 0:
        result.head = result.head.next

    return result
Beispiel #2
0
def sum_lists(head1, head2):
  node1 = head1
  node2 = head2

  result = LinkedList()
  result.insert(0)
  node_res = result.head

  while node1 and node2 and node_res:
    res = node1.data + node2.data + node_res.data
    carry_in = res % 10
    carry_out = int(res / 10)
    node_res.data = carry_in
    new_node = Node(carry_out)
    node_res.next = new_node
    node1 = node1.next
    node2 = node2.next
    node_res = node_res.next

  if node1:
    sum_with_one_list(node1, node_res)
  
  elif node2:
    sum_with_one_list(node2, node_res)

  return remove_last_zero(result.head)
 def __init__(self,
              cats_queue=LinkedList(),
              dogs_queue=LinkedList(),
              last_order=0):
     self.cats_queue = cats_queue
     self.dogs_queue = dogs_queue
     self.last_order = last_order
Beispiel #4
0
def tests():
    ll = LinkedList()
    ll.insert(4)
    ll.insert(3)
    ll.insert(2)
    ll.insert(5)
    ll.insert(2)
    ll.insert(4)
    assert ll.prettify() == [(4, 3), (3, 2), (2, 5), (5, 2), (2, 4), (4, None)]
    remove_dups(ll)
    assert ll.prettify() == [(4, 3), (3, 2), (2, 5), (5, None)]
Beispiel #5
0
def tests():
  ll = LinkedList()
  ll.insert(4)
  ll.insert(3)
  ll.insert(2)
  ll.insert(5)
  ll.insert(2)
  ll.insert(4)
  assert ll.prettify() == [(4, 3), (3, 2), (2, 5), (5, 2), (2, 4), (4, None)]
  assert returnKthToLast(ll, 0).data == 4
  assert returnKthToLast(ll, 2).data == 5
  assert returnKthToLast(ll, 5).data == 4
def tests():
    l1 = LinkedList()
    l1.insert('A')
    l1.insert('B')
    l1.insert('C')
    l1.insert('D')
    l1.insertNode(Node('E', l1.head.next.next))

    assert loop_detection(l1).data == 'C'
def bst_sequences(root):
  results = []
  if not root:
    return results
  
  prefix = LinkedList()
  prefix.insert(root.value)

  left_sequence = bst_sequences(root.left)
  right_sequence = bst_sequences(root.right)

  for left in left_sequence:
    for right in right_sequence:
      res = []
      combine_lists(left, right, res, prefix)
      results.extend(res)
  return results
Beispiel #8
0
def partition(linkedlist, partition):
  partitioned_linked_list = LinkedList()
  node = linkedlist.head

  while node != None:
    if node.data < partition:
      partitioned_linked_list.insertToHead(node.data)
    else:
      partitioned_linked_list.insert(node.data)
  
    node = node.next

  
  return partitioned_linked_list
Beispiel #9
0
def is_palindrome(l):
  middle = l.size() / 2
  count = 0
  node = l.head
  inverted_list = LinkedList()

  while node:
    count += 1
    if count == middle:
      inverted_list = create_inverted_list(node)
    node = node.next
  
  node1 = l.head
  node2 = inverted_list.head
  while node1 and node2:
    if node1.data != node2.data:
      return False
    node1 = node1.next
    node2 = node2.next
  
  return True
Beispiel #10
0
def tests():
  l1 = LinkedList()
  l1.insert(7)
  l1.insert(1)
  l1.insert(6)

  l2 = LinkedList()
  l2.insert(5)
  l2.insert(9)
  l2.insert(2)

  l3 = LinkedList()
  l3.head = sum_lists(l1.head, l2.head)
  assert l3.prettify() == [(2, 1), (1, 9), (9, None)]
Beispiel #11
0
def tests():
    ll = LinkedList()
    ll.insert(3)
    ll.insert(5)
    ll.insert(8)
    ll.insert(5)
    ll.insert(10)
    ll.insert(2)
    ll.insert(1)
    ll.head = partition(ll, 5)
    assert ll.prettify() == [(1, 2), (2, 3), (3, 5), (5, 8), (8, 5), (5, 10),
                             (10, None)]
Beispiel #12
0
def tests():
    l1 = LinkedList()
    l1.insert(2)
    l1.insert(3)
    l1.insert(4)
    l1.insert(5)
    l1.insert(7)

    l2 = LinkedList()
    l2.insert(5)
    l2.insert(6)
    l2.insert(7)
    l2.insert(4)
    l2.insert(5)
    l2.insert(7)

    assert intersection(l1, l2).data == 4
Beispiel #13
0
def tests():
  l1 = LinkedList()
  l1.insert('A')
  l1.insert('N')
  l1.insert('A')

  l2 = LinkedList()
  l2.insert('A')
  l2.insert('N')
  l2.insert('T')
  l2.insert('A')

  assert is_palindrome_2(l1) == True
  assert is_palindrome(l2) == False
Beispiel #14
0
def tests():
  linked_list = LinkedList()
  linked_list.insert(3)
  linked_list.insert(5)
  linked_list.insert(8)
  linked_list.insert(5)
  linked_list.insert(10)
  linked_list.insert(2)
  linked_list.insert(1)

  expected_output = LinkedList()
  expected_output.insert(1)
  expected_output.insert(2)
  expected_output.insert(3)
  expected_output.insert(5)
  expected_output.insert(8)
  expected_output.insert(5)
  expected_output.insert(10)

  assert expected_output.prettify() == partition(linked_list, 5).prettify()
def tests():
    l1 = LinkedList()
    l1.insert(6)
    l1.insert(1)
    l1.insert(7)

    l2 = LinkedList()
    l2.insert(2)
    l2.insert(9)
    l2.insert(5)

    l3 = sum_lists(l1, l2)
    print(l3.prettify())
Beispiel #16
0
def tests():
  l1 = LinkedList()
  l1.insert('A')
  l1.insert('N')
  l1.insert('A')

  l2 = LinkedList()
  l2.insert('A')
  l2.insert('N')
  l2.insert('T')
  l2.insert('A')
Beispiel #17
0
def list_of_depths(root):
    array = []
    linked_list = LinkedList()
    if root.value:
        linked_list.insert(root)  # Visiting the root

    while linked_list.size() > 0:
        array.append(
            linked_list)  # Adding the level to the array of linkedLists
        parents = linked_list  # Storing the previous level
        linked_list = LinkedList()  # Reseting the linkedList of current level
        parent = parents.head
        while parent:
            p = parent.data
            if p.left and p.left.value:
                linked_list.insert(p.left)

            if p.right and p.right.value:
                linked_list.insert(p.right)
            parent = parent.next

    return array
Beispiel #18
0
def create_inverted_list(node):
  list = LinkedList()
  while node:
    list.insertToHead(node)
    node = node.next
  return list