def reverse(linked_list):
    """
    Reverse the inputted linked list

    Args:
       linked_list(obj): Linked List to be reversed
    Returns:
       obj: Reveresed Linked List
    """

    new_list = LinkedList()

    node = deepcopy(linked_list.head)

    # A bit of a complex operation here. We want to take the
    # node from the original linked list and prepend it to
    # the new linked list
    while node:
        old_head = new_list.head
        new_node = node
        node = node.next

        new_node.next = old_head
        new_list.head = new_node
    return new_list
Beispiel #2
0
def create_linked_list_with_list_of_nodes(nodes):
    linked_list = LinkedList(nodes[0])  # head
    for node in nodes[1:]:
        linked_list.insert(node)
    return linked_list
Beispiel #3
0
def create_linked_list_with_list_of_values(values):
    linked_list = LinkedList(Node(values[0]))  # head
    for value in values[1:]:
        linked_list.insert(Node(value))
    return linked_list
Beispiel #4
0
 def setUp(self):
     head = Node(1)
     self.linked_list = LinkedList(head)
Beispiel #5
0
class TestLinkedList(unittest.TestCase):
    def setUp(self):
        head = Node(1)
        self.linked_list = LinkedList(head)

    def test_insert(self):
        new_node = Node(2)
        self.linked_list.insert(new_node)
        expected = 2
        result = self.linked_list.get_last_node().value
        assert_that(expected).is_equal_to(result)

    def test_find(self):
        self.linked_list.insert(Node(3))
        self.linked_list.insert(Node(4))
        result = self.linked_list.find(3)
        expected = Node(3)
        assert_that(expected.value).is_equal_to(result.value)

    def test_remove(self):
        self.linked_list.insert(Node(5))
        self.linked_list.insert(Node(6))
        result = self.linked_list.remove(5)
        assert_that(result).is_true()

    def test_remove_non_existing(self):
        self.linked_list.insert(Node(5))
        self.linked_list.insert(Node(6))
        with self.assertRaises(ValueError):
            self.linked_list.remove(100)

    def test_iteration(self):
        self.linked_list.insert(Node(2))
        self.linked_list.insert(Node(3))
        self.linked_list.insert(Node(4))
        numbers = [1, 2, 3, 4]
        for val, num in zip(self.linked_list, numbers):
            assert_that(val).is_equal_to(num)

    def test_length(self):
        self.linked_list.insert(Node(2))
        self.linked_list.insert(Node(3))
        self.linked_list.insert(Node(4))
        self.linked_list.insert(Node(5))
        self.linked_list.insert(Node(6))
        assert_that(6).is_equal_to(len(self.linked_list))

    def test_get_nth_node(self):
        nth = 3
        self.linked_list.insert(Node(2))
        self.linked_list.insert(Node(3))
        self.linked_list.insert(Node(4))
        assert_that(nth).is_equal_to(self.linked_list.get_nth_node(nth).value)
    Reverse the inputted linked list

    Args:
       linked_list(obj): Linked List to be reversed
    Returns:
       obj: Reveresed Linked List
    """

    new_list = LinkedList()

    node = deepcopy(linked_list.head)

    # A bit of a complex operation here. We want to take the
    # node from the original linked list and prepend it to
    # the new linked list
    while node:
        old_head = new_list.head
        new_node = node
        node = node.next

        new_node.next = old_head
        new_list.head = new_node
    return new_list


list1 = LinkedList()
for value in [4, 2, 5, 1, -3, 0]:
    list1.append(value)

print("Pass" if (list(list1) == list(reverse(reverse(list1)))) else "Fail")
Beispiel #7
0
        # if a node refers to itself, that's a little loop
        if slow.next == slow:
            return True

        # slow pointer moves one node
        slow = slow.next
        # fast pointer moves two nodes
        fast = fast.next.next

        if slow == fast:
            return True

    # If we get to a node where fast doesn't have a next node or doesn't exist itself, 
    # the list has an end and isn't circular
    return False


list_with_loop = LinkedList([2, -1, 3, 0, 5])

# Creating a loop where the last node points back to the second node
loop_start = list_with_loop.head.next

node = list_with_loop.head
while node.next:
    node = node.next
node.next = loop_start

print("Pass" if (is_circular(list_with_loop) is True) else "Fail")
print("Pass" if (is_circular(LinkedList([-4, 7, 2, 5, -1])) is False) else "Fail")
print("Pass" if (is_circular(LinkedList([1])) is False) else "Fail")
 def __init__(self, max_len=10):
     self._array = [LinkedList() for _ in range(max_len)]
     self._max_len = max_len