def test_pop(): colors = SingleLinkedList() colors.push("Magenta") colors.push("Alizarin") assert colors.pop() == "Alizarin" assert colors.pop() == "Magenta" assert colors.pop() == None
def test_last(): colors = SingleLinkedList() colors.push("Cadmium Red Light") assert colors.last() == "Cadmium Red Light" colors.push("Hansa Yellow") assert colors.last() == "Hansa Yellow" colors.shift("Pthalo Green") assert colors.last() == "Hansa Yellow"
def test_unshift(): colors = SingleLinkedList() colors.push("Viridian") colors.push("Sap Green") colors.push("Van Dyke") assert colors.unshift() == "Viridian" assert colors.unshift() == "Sap Green" assert colors.unshift() == "Van Dyke" assert colors.unshift() == None
def test_remove(): colors = SingleLinkedList() colors.push("Cobalt") colors.push("Zinc White") colors.push("Nickle Yellow") colors.push("Perinone") assert colors.remove("Cobalt") == 0 assert colors.remove("Perinone") == 2 assert colors.remove("Nickle Yellow") == 1 assert colors.remove("Zinc White") == 0
def test_shift(): colors = SingleLinkedList() colors.shift("Cadmium Orange") assert colors.count() == 1 colors.shift("Carbazole Violet") assert colors.count() == 2 assert colors.pop() == "Cadmium Orange" assert colors.count() == 1 assert colors.pop() == "Carbazole Violet" assert colors.count() == 0
def test_remove(): colors = SingleLinkedList() colors.push("Cobalt") colors.push("Zinc White") colors.push("Nickle Yellow") colors.push("Perinone") assert colors.remove("Cobalt") == 0 colors.dump("before perinone") assert colors.remove("Perinone") == 2 colors.dump("after perinone") assert colors.remove("Nickle Yellow") == 1 assert colors.remove("Zinc White") == 0
def test_single_linked_list(): # FillConstant @pytest.fixture def element_A(): return { "event_tracking_name": "VISIT", "event_trigger": "HOMEPAGE", "client_timestamp": 12879818279387, } node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(3) node4 = ListNode(4) node5 = ListNode(5) node6 = ListNode(6) node7 = ListNode(7) node8 = ListNode(8) node9 = ListNode(9) nodeA = ListNode("A") nodeB = ListNode("B") nodeC = ListNode("C") link1 = SingleLinkedList() link1.add_list_item(node1) link1.add_list_item(node2) link1.add_list_item(node3) link1.add_list_item(node4) link1.add_list_item(node5) link1.add_list_item(node6) link1.add_list_item(node7) link1.add_list_item(node8) link1.add_list_item(node9) assert link1.list_length() == 9 print("remove value 5") link1.remove_by_value(5) assert link1.list_length() == 8 assert link1.head.data == 1 assert link1.tail.data == 9 link1.remove_by_value(9) assert link1.list_length() == 7 assert link1.head.data == 1 assert link1.tail.data == 8 res = link1.search(3) assert res == [3]
def test_get(): colors = SingleLinkedList() colors.push("Vermillion") assert colors.get(0) == "Vermillion" colors.push("Sap Green") assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" colors.push("Cadmium Yellow Light") assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" assert colors.get(2) == "Cadmium Yellow Light" assert colors.pop() == "Cadmium Yellow Light" assert colors.get(0) == "Vermillion" assert colors.get(1) == "Sap Green" assert colors.get(2) == None colors.pop() assert colors.get(0) == "Vermillion" colors.pop() assert colors.get(0) == None
def get_loop_linked_list(self, n): sll = SingleLinkedList() head = sll.add_item_from_arr(list(range(n))) if head is None: return None, None ptr = head while ptr.next: ptr = ptr.next ptr.next = head return head, ptr ## 直接用list来模拟环形链表,找到删除数字之间的下标变化规律 def LastRemaining_Solution(self, n, m): if n < 1 or m < 1: return -1 arr = list(range(n)) index = (m - 1) % len(arr) while len(arr) > 1: arr.pop(index) index = (index + m - 1) % len(arr) return arr[0]
def _createHashTable(self): for i in range(self.size): self.buckets.append(SingleLinkedList())
def test_push(): colors = SingleLinkedList() colors.push("Pthalo Blue") assert colors.count() == 1 colors.push("Ultramarine Blue") assert colors.count() == 2
# 迭代解法 def ReverseList(self, pHead): # write code here p_reverse_head = None cur_node = pHead pre_node = None while cur_node is not None: next_node = cur_node.next ## 需要提前保存好下一个节点的值 if next_node is None: p_reverse_head = cur_node cur_node.next = pre_node pre_node = cur_node cur_node = next_node return p_reverse_head ## 递归解法 def ReverseList(self, pHead): if not pHead or not pHead.next: return pHead else: pReversedHead = self.ReverseList(pHead.next) pHead.next.next = pHead ## 把None换掉 pHead.next = None ## 最后要接一个None return pReversedHead sll = SingleLinkedList() sll.add_item_from_arr([1, 2, 3, 4]) sol = Solution() res = sol.ReverseList(sll.head) pass
def setUp(self): self.node1 = Node(1) self.node2 = Node(2) self.node3 = Node(3) self.linked_list = SingleLinkedList()
def __init__(self): # initialize a linked list to store item self.linked_list = SingleLinkedList() return