def test_remove_raises_exception_when_node_not_found(): """Test that exception is raised when node isn't found.""" ll = LinkedList() assert ll.remove(ll.head) == "Node was not found in list"
def __init__(self): self.ll = LinkedList() self.size = self.ll.count
def test_list_length_equals_1(): happy_pants = LinkedList() happy_pants.insert(2) actual = happy_pants.kthFromEnd(0) expected = 2 assert actual == expected
def test_flip() -> None: node_1_data = {"id": 1} node_2_data = {"id": 2} node_3_data = {"id": 3} node_4_data = {"id": 4} node_5_data = {"id": 5} linked_list = LinkedList() linked_list.append(node_1_data) linked_list.append(node_2_data) linked_list.append(node_3_data) linked_list.append(node_4_data) linked_list.append(node_5_data) # non-consecutive nodes: # 1 2 3 4 5 -> 1 4 3 2 5 linked_list.flip(linked_list.head.next_node, linked_list.tail.previous_node, "id") first_node = linked_list.head assert first_node.data == node_1_data assert first_node.previous_node is None assert first_node.next_node.data == node_4_data second_node = linked_list.head.next_node assert second_node is not None assert second_node.data == node_4_data assert second_node.previous_node.data == node_1_data assert second_node.next_node.data == node_3_data third_node = second_node.next_node assert third_node is not None assert third_node.data == node_3_data assert third_node.previous_node.data == node_4_data assert third_node.next_node.data == node_2_data fourth_node = third_node.next_node assert fourth_node is not None assert fourth_node.data == node_2_data assert fourth_node.previous_node.data == node_3_data assert fourth_node.next_node.data == node_5_data fifth_node = fourth_node.next_node assert fifth_node is not None assert fifth_node.data == node_5_data assert fifth_node.previous_node.data == node_2_data assert fifth_node.next_node is None assert linked_list.tail.data == node_5_data # consecutive nodes: # 1 4 3 2 5 -> 1 4 2 3 5 linked_list.flip(linked_list.tail.previous_node.previous_node, linked_list.tail.previous_node, "id") first_node = linked_list.head assert first_node.data == node_1_data assert first_node.previous_node is None assert first_node.next_node.data == node_4_data second_node = linked_list.head.next_node assert second_node is not None assert second_node.data == node_4_data assert second_node.previous_node.data == node_1_data assert second_node.next_node.data == node_2_data third_node = second_node.next_node assert third_node is not None assert third_node.data == node_2_data assert third_node.previous_node.data == node_4_data assert third_node.next_node.data == node_3_data fourth_node = third_node.next_node assert fourth_node is not None assert fourth_node.data == node_3_data assert fourth_node.previous_node.data == node_2_data assert fourth_node.next_node.data == node_5_data fifth_node = fourth_node.next_node assert fifth_node is not None assert fifth_node.data == node_5_data assert fifth_node.previous_node.data == node_3_data assert fifth_node.next_node is None assert linked_list.tail.data == node_5_data # A = head B = tail # 1 4 3 2 5 -> 5 4 2 3 1 linked_list.flip(linked_list.head, linked_list.tail, "id") first_node = linked_list.head assert first_node.data == node_5_data assert first_node.previous_node is None assert first_node.next_node.data == node_4_data second_node = linked_list.head.next_node assert second_node is not None assert second_node.data == node_4_data assert second_node.previous_node.data == node_5_data assert second_node.next_node.data == node_2_data third_node = second_node.next_node assert third_node is not None assert third_node.data == node_2_data assert third_node.previous_node.data == node_4_data assert third_node.next_node.data == node_3_data fourth_node = third_node.next_node assert fourth_node is not None assert fourth_node.data == node_3_data assert fourth_node.previous_node.data == node_2_data assert fourth_node.next_node.data == node_1_data fifth_node = fourth_node.next_node assert fifth_node is not None assert fifth_node.data == node_1_data assert fifth_node.previous_node.data == node_3_data assert fifth_node.next_node is None assert linked_list.tail.data == node_1_data
def test_sorting() -> None: def node_comparison_function(node): return int(node.data["id"]) if node else 0 node_1_data = {"id": 1} node_2_data = {"id": 2} node_3_data = {"id": 3} node_4_data = {"id": 4} linked_list = LinkedList() linked_list.append(node_4_data) linked_list.append(node_3_data) linked_list.append(node_2_data) linked_list.append(node_1_data) # 4 3 2 1 -> 1 2 3 4 linked_list.sort(comparison_function=node_comparison_function) first_node = linked_list.head assert first_node.data == node_1_data assert first_node.previous_node is None assert first_node.next_node.data == node_2_data second_node = linked_list.head.next_node assert second_node is not None assert second_node.data == node_2_data assert second_node.previous_node.data == node_1_data assert second_node.next_node.data == node_3_data third_node = second_node.next_node assert third_node is not None assert third_node.data == node_3_data assert third_node.previous_node.data == node_2_data assert third_node.next_node.data == node_4_data fourth_node = third_node.next_node assert fourth_node is not None assert fourth_node.data == node_4_data assert fourth_node.previous_node.data == node_3_data assert fourth_node.next_node is None assert linked_list.tail.data == node_4_data linked_list = LinkedList() linked_list.append(node_1_data) linked_list.append(node_2_data) linked_list.append(node_3_data) linked_list.append(node_4_data) # 1 2 3 4 -> 4 3 2 1 linked_list.sort(comparison_function=node_comparison_function, reverse=True) first_node = linked_list.head assert first_node.data == node_4_data assert first_node.previous_node is None assert first_node.next_node.data == node_3_data second_node = linked_list.head.next_node assert second_node is not None assert second_node.data == node_3_data assert second_node.previous_node.data == node_4_data assert second_node.next_node.data == node_2_data third_node = second_node.next_node assert third_node is not None assert third_node.data == node_2_data assert third_node.previous_node.data == node_3_data assert third_node.next_node.data == node_1_data fourth_node = third_node.next_node assert fourth_node is not None assert fourth_node.data == node_1_data assert fourth_node.previous_node.data == node_2_data assert fourth_node.next_node is None assert linked_list.tail.data == node_1_data
def add(self, key, value): hashed_key_index = self._hash(key) if not self._buckets[hashed_key_index]: self._buckets[hashed_key_index] = LinkedList() self._buckets[hashed_key_index].append((key, value))
def setUp(self): self.ll = LinkedList()
def test_empty_list_length(): """Test size() on empty list.""" test_list = LinkedList() assert test_list.size() == 0
def test_length_with_two_nodes(): """test_length_with_two_nodes.""" test_list = LinkedList() test_list.push(2) test_list.push(3) assert test_list.size() == 2
def test_push_two_items_head_next(): """Test for push method.""" res = LinkedList() res.push(2) res.push(3) assert res.head.next_node.val == 2
def test_init_list(): """"test_init_list.""" init = LinkedList() assert init.head is None
def test_ll_search_one_val(): """Test search method with one node.""" ll = LinkedList() ll.push(2) assert ll.search(2).val == 2
def test_linkedlist_take_iterable(): """Test that LinkedList can take an iterable.""" tlist = [5, 4, 3, 2, 1] ll = LinkedList(tlist) for item in tlist: assert ll.search(item).val == item
def test_pop_raises_exception_when_node_not_found(): """Test that exception is raised when node isn't found.""" ll = LinkedList() assert ll.pop() == "List is empty, cannot pop from an empty list"
temp1 = temp1.next else: temp = temp2 temp2 = temp2.next temp3.next = temp temp3 = temp if temp1: temp3.next = temp1 else: temp3.next = temp2 return head3 if __name__ == '__main__': linked_list1 = LinkedList() linked_list2 = LinkedList() llist1 = [1, 5, 7, 8] llist2 = [2, 3, 4, 6] linked_list1.create_linked_list(input_list=llist1) linked_list2.create_linked_list(input_list=llist2) print("First linkedlist") linked_list1.print_list() print("Second linkedlist") linked_list2.print_list() head1 = linked_list1.head head2 = linked_list2.head new_head = merge_linked_lists(head1=head1, head2=head2) print("merged list is") new_list = LinkedList(head=new_head) new_list.print_list()
def test_push(): """Test for push method.""" res = LinkedList() res.push(2) assert res.head.val == 2
def __init__(self): self.size = 0 self.storage = LinkedList()
def test_empty_list_length_with_len(): """Test size() on empty list using len method.""" test_list = LinkedList() assert len(test_list) == 0
def test_size(self): self.ll = LinkedList(10) self.assertEqual(self.ll.size(), 10)
def test_linked_list_pop_removes_head(): """Test to see if pop method removes the current head.""" ll = LinkedList() ll.push(1) ll.pop() assert ll.head is None
def test_reversing_empty_list() -> None: linked_list = LinkedList() linked_list.reverse() assert linked_list.head is None assert linked_list.tail is None
if not p2.next: even = False break if not p2.next.next: break p1 = p1.next p2 = p2.next.next if not even: stack.pop() while p1.next: p1 = p1.next if p1.value != stack.pop(): return False return True if __name__ == '__main__': a = ListNode(3) b = ListNode(2, next_node=a) c = ListNode(7, next_node=b) d = ListNode(2, next_node=c) e = ListNode(3, next_node=d) ll = LinkedList(head=e) print ll print is_palindrom(ll)
def test_initial_list_state() -> None: linked_list = LinkedList() assert linked_list.head is None assert linked_list.tail is None
from linked_list import Node from linked_list import LinkedList def delete_at_beginning(head): temp = head next_node = temp.next temp.next = None return next_node if __name__ == '__main__': linked_list = LinkedList() llist = [1, 2, 3, 4] linked_list.create_linked_list(input_list=llist) print("Linked list before") linked_list.print_list() current_head = linked_list.head changed_head = delete_at_beginning(head=current_head) linked_list.head = changed_head print("Linked list after") linked_list.print_list()
def __init__(self, initPos: Cell): self.head = initPos #head: saves row and col of head self.body = LinkedList(self.head)
from linked_list import Node, LinkedList ll = LinkedList(range(1, 11)) to_remove = [2, 1, 8] print 'Original:' ll.output() for i in to_remove: print 'Removed ' + str(i) + ':' ll.remove(i) ll.output()
# time: O(n) # space: O(1) from linked_list import LinkedList, Node # a simple linked list module in this folder def delete_node(node: Node) -> None: ''' Overwrite each value of a linked list, starting from the node we are given to delete. ''' while node.next != None: node.val = node.next.val if node.next.next == None: node.next = None else: node = node.next # Examples: head1 = LinkedList([4, 5, 1, 9]).head delete_node(head1.next) # 4 -> 1 -> 9 print(head1.val == 4 and head1.next.val == 1 and head1.next.next.val == 9 and head1.next.next.next == None) head2 = LinkedList([4, 5, 1, 9]).head delete_node(head2.next.next) # 4 -> 5 -> 9 print(head2.val == 4 and head2.next.val == 5 and head2.next.next.val == 9 and head2.next.next.next == None)
def reverse(ll): """ docstring """ prev = None curr = ll.head while curr is not None: temp = curr.next curr.next = prev prev = curr curr = temp ll.head = prev if __name__ == "__main__": n = int(sys.argv[1]) items = [] for _ in range(n): items.append(random.randrange(151)) ll = LinkedList() for item in items: ll.push(Node(item)) print_nodes(ll) print() reverse(ll) print_nodes(ll)
def __init__(self): # initializes size of queue self.size = 0 # initializes items in storage self.storage = LinkedList()
def test_push_two_items(): """Test for push method.""" res = LinkedList() res.push(2) res.push(3) assert res.head.val == 3