예제 #1
0
    def test_not_palindrome(self):
        linked_list = LinkedList(None)
        for value in self.not_palindrome_values:
            linked_list.append_to_tail(value)
        linked_list.head = linked_list.head.next

        self.assertFalse(is_palindrome(linked_list))
예제 #2
0
    def test_partition(self):
        linked_list = LinkedList(None)
        for value in self.values:
            linked_list.append_to_tail(value)
        linked_list.head = linked_list.head.next
        linked_list.head = partition(linked_list, self.partition)

        for value in self.expected:
            self.assertEqual(value, linked_list.head.value)
            linked_list.head = linked_list.head.next
예제 #3
0
    def test_return_kth_element(self):
        linked_list = LinkedList(None)
        for value in self.values:
            linked_list.append_to_tail(value)
        linked_list.head = linked_list.head.next

        for i in range(len(self.values)):
            self.assertEqual(
                self.values[i],
                return_kth_element(linked_list,
                                   len(self.values) - i))
예제 #4
0
    def test_remove_duplicates(self):
        linked_list = LinkedList(None)
        for value in self.values:
            linked_list.append_to_tail(value)

        linked_list.head = linked_list.head.next
        remove_duplicates(linked_list)

        for value in self.result:
            self.assertEqual(value, linked_list.head.value)
            linked_list.head = linked_list.head.next
예제 #5
0
    def test_intersection(self):
        list_1 = LinkedList(None)
        for value in self.l1_values:
            list_1.append_to_tail(value)
        list_1.head = list_1.head.next

        list_2 = LinkedList(None)
        for value in self.l2_values:
            list_2.append_to_tail(value)
        list_2.head = list_2.head.next

        self.assertEqual(self.expected, intersection(list_1, list_2))
예제 #6
0
    def test_delete_middle_node(self):
        linked_list = LinkedList(None)
        for value in self.values:
            linked_list.append_to_tail(value)
        linked_list.head = linked_list.head.next

        middle_node = linked_list.head
        while middle_node.value != self.middle_node_value:
            middle_node = middle_node.next
        delete_middle_node(middle_node)

        for value in self.expected:
            self.assertEqual(value, linked_list.head.value)
            linked_list.head = linked_list.head.next
예제 #7
0
    def test_sum_lists_forward(self):
        list_1 = LinkedList(None)
        for value in self.list1_values[::-1]:
            list_1.append_to_tail(value)
        list_1.head = list_1.head.next

        list_2 = LinkedList(None)
        for value in self.list2_values[::-1]:
            list_2.append_to_tail(value)
        list_2.head = list_2.head.next

        result = sum_lists_forward(list_1, list_2)
        for value in self.expected[::-1]:
            self.assertEqual(value, result.head.value)
            result.head = result.head.next
예제 #8
0
def sum_lists(list_1, list_2):
    result = LinkedList(None)
    carry = 0
    while list_1.head is not None and list_2.head is not None:
        if list_1.head is None:
            value = list_2.head.value + carry
        elif list_2.head is None:
            value = list_1.head.value + carry
        else:
            value = list_1.head.value + list_2.head.value + carry

        carry = value // 10
        result.append_to_tail(value % 10)
        list_1.head = list_1.head.next
        list_2.head = list_2.head.next

    result.head = result.head.next
    return result
예제 #9
0
    def test_loop_detection(self):
        loop_list = LinkedList(None)
        for value in self.loop_values:
            loop_list.append_to_tail(value)
        loop_list.head = loop_list.head.next

        node = LinkedListNode('C')
        current = loop_list.head
        while current.value != 'B':
            current = current.next
        node.next = current.next.next
        current.next = node

        while current.next is not None:
            current = current.next
        current.next = node

        self.assertEqual(self.expected, loop_detection(loop_list))
        self.assertEqual(self.expected, loop_detection_no_extra_space(loop_list))
예제 #10
0
def sum_lists_forward(list_1, list_2):
    result = LinkedList(None)
    prev = result.head
    carry = 0
    while list_1.head is not None and list_2.head is not None:
        if list_1.head is None:
            value = list_2.head.value
        elif list_2.head is None:
            value = list_1.head.value
        else:
            value = list_1.head.value + list_2.head.value

        result.append_to_tail(value % 10)
        carry = value // 10
        if prev.value is None:
            prev = prev.next
        else:
            prev.value += carry
            prev = prev.next
        list_1.head = list_1.head.next
        list_2.head = list_2.head.next

    result.head = result.head.next
    return result
예제 #11
0
            list2 = list2.next
            idx += 1
    import ipdb
    ipdb.set_trace()
    # we traverse on each list until the pointers are the same
    while list1 is not None:
        if list1 == list2:
            return list1
        list1 = list1.next
        list2 = list2.next


if __name__ == "__main__":
    list_1 = LinkedList()
    list_1.head = Node("3")
    list_1.append_to_tail(Node("4"))

    list_2 = LinkedList()
    list_2.head = Node("3")
    list_2.append_to_tail(Node("4"))
    list_2.append_to_tail(Node("6"))
    list_2.append_to_tail(Node("6"))
    list_2.append_to_tail(Node("6"))
    list_2.append_to_tail(Node("4"))
    list_2.append_to_tail(Node("3"))

    intersecting_node1 = Node("5")
    intersecting_node2 = Node("6")

    list_1.append_to_tail(intersecting_node1)
    #list_1.append_to_tail(intersecting_node2)
"""

from linked_list import LinkedList, Node


# Recursive way
# Iterate through the list
# When it hits the end, the method passes back a counter set to 0
# Each parent call increases by 1 the counter
def return_kth_to_last_element(self, head, k):
    if head.next_val == None:
        return 0
    index = self.return_kth_to_last_element(head.next_val, k) + 1
    if index == k:
        print('Head ' + head.data_val)
    return index


if __name__ == "__main__":
    list_ = LinkedList()
    list_.head = Node("3")
    list_.append_to_tail(Node("1"))
    list_.append_to_tail(Node("2"))
    list_.append_to_tail(Node("4"))
    list_.append_to_tail(Node("1"))
    list_.append_to_tail(Node("2"))
    list_.append_to_tail(Node("4"))

    list_.printList()
    return_kth_to_last_element(list_.head, 4)
예제 #13
0
def sum_lists(self, head1, head2, carry):
    if head1 is None and head2 is None and carry == 0:
        return None
    value = int(carry)
    if head1 is not None:
        value += int(head1.data_val)
    if head2 is not None:
        value += int(head2.data_val)

    node = Node(value % 10)
    node.next = self.sum_lists(
        head1.next_val if head1.next_val is not None else None,
        head2.next_val if head2.next_val is not None else None,
        1 if value > 10 else 0)
    return node

if __name__ == "__main__":
    list_1 = LinkedList()
    list_1.head = Node("4")
    list_1.append_to_tail(Node("5"))
    list_1.append_to_tail(Node("6"))

    list_2 = LinkedList()
    list_2.head = Node("3")
    list_2.append_to_tail(Node("1"))
    list_2.append_to_tail(Node("2"))

    list_1.printList()
    list_2.printList()
    return_kth_to_last_element(list_1.head, list_2.head)
예제 #14
0
        fast = fast.next.next
        slow = slow.next
        if fast == slow:
            break

    # Error check - no meeting point
    if fast is None or fast.next is None:
        return False

    # If they move at the same pace, they must meet at loop start
    slow = list_
    while slow != fast:
        slow = slow.next
        fast = fast.next

    return fast


if __name__ == "__main__":
    list_ = LinkedList()
    list_.head = Node("3")
    list_.append_to_tail(Node("1"))
    list_.append_to_tail(Node("2"))
    loop_node = Node("5")
    list_.append_to_tail(loop_node)
    list_.append_to_tail(Node("4"))
    list_.append_to_tail(loop_node)

    list_.printList()
    loop_detection(list_.head)
# Uses two pinters to keep trak of the current location
# Worst case running time is O(n)
def remove_duplicates_without_set(head):
    n = head
    while n.next is not None:
        m = n
        while m.next is not None:
            if n.data == m.next.data:
                m.next = m.next.next
            else:
                m = m.next
        n = n.next


if __name__ == "__main__":
    list_ = LinkedList()
    list_.head = Node("3")
    list_.append_to_tail(Node("4"))
    list_.append_to_tail(Node("6"))
    list_.append_to_tail(Node("4"))
    list_.append_to_tail(Node("8"))
    list_.append_to_tail(Node("9"))
    c = Node("1")
    list_.append_to_tail(c)
    print("{}".format(list_))
    list_.printList()
    remove_duplicates_with_set(list_.head)
    print("{}".format(list_))
    list_.printList()