Beispiel #1
0
def main():
    l_list = LinkedList()
    l_list.generate_list(5, 1, 20)
    node = find_node(l_list,3)
    print(f'Source List: {l_list}')
    delete_nth_node(node)
    print(f'Deleted list: {l_list}')
Beispiel #2
0
class TestPalindromeList(unittest.TestCase):
    """
    Test cases for a Palindrome Linked List
    """

    _list_true = LinkedList()
    _list_true.add_last(1)
    _list_true.add_last(2)
    _list_true.add_last(3)
    _list_true.add_last(4)
    _list_true.add_last(3)
    _list_true.add_last(2)
    _list_true.add_last(1)

    _list_false = LinkedList()
    _list_false.add_last(1)
    _list_false.add_last(2)
    _list_false.add_last(3)
    _list_false.add_last(4)
    _list_false.add_last(5)

    def test_palindrome(self):
        _, res = is_palindrome_recurse(self._list_true.head(),
                                       len(self._list_true))
        self.assertTrue(res)

    def test_not_palindrome(self):
        _, res = is_palindrome_recurse(self._list_false.head(),
                                       len(self._list_false))
        self.assertFalse(res)
Beispiel #3
0
def is_palindrome_stack(l_list: LinkedList):
    """
    Implement the algorithm to check if the linked list is a palindrome using a stack
    Use the Fast Pointer, Slow pointer technique to insert the first half of the list in the stack,
    pop an item off the stack and traverse through the remaining list to see if they match.
    1 -> 2 -> 3 -> 4 -> 3 -> 2 -> 1 - True
    1 -> 2 -> 3 -> 4 -> 4 -> 3 -> 2 -> 1 - True
    :param l_list: Linked list
    :return: True if the list is a palindrome, False otherwise
    """
    if not l_list or l_list.empty():
        return False
    # Traverse through the first half of the list and add to the stack
    stack = []
    slow = l_list.head()
    fast = l_list.head()

    while fast and fast.next():
        stack.append(slow.value())
        slow = slow.next()
        fast = fast.next().next()

    if fast:  # If there are odd number of elements in the list, the fast pointer is not null
        slow = slow.next()
    # Start popping from the stack and compare against the remaining elements in the list using the slow pointer
    while slow:
        item = stack.pop()
        if not item == slow.value():
            return False
        slow = slow.next()

    return True
Beispiel #4
0
def main():
    l_list = LinkedList()
    # l_list.add_last(30)
    # l_list.add_last(10)
    # l_list.add_last(15)
    # l_list.add_last(5)
    # l_list.add_last(1)
    l_list.generate_list(5, 1, 20)
    print(partition(l_list, 7))
Beispiel #5
0
def main():
    """
    Responsible for creating a linked list and calling remove_dups
    :return:
    """
    l_list = LinkedList()
    l_list.add_to_list([1, 2, 3, 2])
    # remove_dups(l_list)
    remove_dups_followup(l_list)
    print(l_list)
Beispiel #6
0
def _sum_list_helper(l1, l2):
    if not l1 and not l2:
        return 0, None

    carry, node = _sum_list_helper(l1.next() if l1 else None,
                                   l2.next() if l2 else None)

    node = LinkedList() if not node else node
    node_sum = carry + (l1.value() if l1 else 0) + (l2.value() if l2 else 0)
    node.add_first(node_sum % 10)
    return node_sum // 10, node
Beispiel #7
0
def sum_lists(l1, l2, carry):
    """
    You have two numbers represented by a linked list, where each node contains a single
    digit. The digits are stored in reverse order, such that the 1 's digit is at the head of the list. Write a
    function that adds the two numbers and returns the sum as a linked list.
    EXAMPLE
        Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is,617 + 295.
        Output: 2 -> 1 -> 9. That is, 912.
        Input: (8-> 3) + (1 -> 1 -> 2).That is,211 + 38.
        Output: 9 -> 4 -> 2. That is, 249.
    :param l1: List 1
    :param l2: List 2
    :param carry: Carry over the sum in the current execution thread
    :return:
    """
    if not l1 and not l2 and carry == 0:
        return

    new_list = LinkedList()
    node_sum = carry + (l1.value() if l1 else 0) + (l2.value() if l2 else 0)
    new_list.add_last(node_sum % 10)
    result = sum_lists(l1.next() if l1 else None,
                       l2.next() if l2 else None, node_sum // 10)
    new_list.add_last(result) if result else None

    return new_list
Beispiel #8
0
def main():
    l1 = LinkedList()
    # l1.add_last(8)
    # l1.add_last(3)
    #
    # l2 = LinkedList()
    # l2.add_last(1)
    # l2.add_last(1)
    # l2.add_last(2)
    #
    # new_list = sum_lists(l1.head(), l2.head(), 0)

    l1.generate_list(3, 7, 9)
    l2 = LinkedList()
    l2.generate_list(3, 7, 9)
    print(f'List 1 : {l1}')
    print(f'List 2 : {l2}')
    new_list = sum_lists(l1, l2)
    print(new_list)
Beispiel #9
0
def main():
    l_list = LinkedList()
    l_list.generate_list(5, 1, 15)
    print(l_list)
    # print(kth_to_last(l_list, 3))
    kth_to_last_recursive(l_list.head(), 4)
Beispiel #10
0
def partition(l_list, part_key):
    """
    Write code to partition a linked list around a value x, such that all nodes less than x come
    before all nodes greater than or equal to x. If x is contained within the list the values of x only need
    to be after the elements less than x (see below). The partition element x can appear anywhere in the
    "right partition"; it does not need to appear between the left and right partitions.
    :param l_list: Linked List to be partitioned
    :param part_key:Key around which the linked list should be partitioned
    :return: Reference to the newly created list
    """
    if not l_list:
        return
    left_start = left_end = right_start = None

    curr = l_list.head()
    left_start = LinkedList()
    right_start = LinkedList()
    while curr:
        if curr.value() < part_key:
            left_start.add_last(curr)
        else:
            right_start.add_last(curr)
        curr = curr.next()

    if not left_start.empty():
        left_end = left_start.tail()
        left_end._next = right_start.head()
    return left_start if not left_start.empty() else right_start