def to_linked_list(s: str) -> Node: head = Node(s[0]) tail = s[1:] p = head for c in tail: current = Node(c) p.next = current p = current return head
def test_node(self): # can be instantiated head = Node('head') self.assertTrue(head.next == None) self.assertTrue(head.record == 'head') # can set pointer to next next = Node('next') head.set_next(next) self.assertTrue(head.next == next)
def remove(head: Node, to_remove: Node) -> Node: """ Remove a node from the middle of a list given only the pointer to that node "Absorb" the next node into the node to be removed. Then simply skip the next node. """ temp: Node = to_remove.next to_remove.record = temp.record to_remove.set_next(temp.next) return head
def test_linked_list(self): first = Node('first') second = Node('second') third = Node('third') first.next = second second.next = third self.assertTrue(first.next == second) self.assertTrue(second.next == third)
print("No cycle found") return if visited.__contains__(n): print('Found cycle at {}'.format(n.record)) return if n is not None: visited.append(n) recurse(access_next(n), visited) # Test cycle print("Testing cycle...") repeated = Node('c') e = Node('e') e.set_next(repeated) d = Node('d') d.set_next(e) repeated.set_next(d) b = Node('b') b.set_next(repeated) a = Node('a') a.set_next(b) recurse(a) # Test no cycle print("Testing no cycle...") c = Node('c') b = Node('b')
def setUp(self): self.a = Node('a') self.b = Node('b') self.c = Node('c') self.d = Node('d') self.e = Node('e')
class TestTwoSix(unittest.TestCase): def setUp(self): self.a = Node('a') self.b = Node('b') self.c = Node('c') self.d = Node('d') self.e = Node('e') def test_find_cycle(self): self.a.set_next(self.b) self.b.set_next(self.c) self.c.set_next(self.d) self.d.set_next(self.e) self.e.set_next(self.c) head = self.a cycle = self.c self.assertEqual(tester.find_cycle(head), cycle) def test_no_cycle(self): self.a.set_next(self.b) self.b.set_next(self.c) self.c.set_next(self.d) self.d.set_next(self.e) self.assertEqual(tester.find_cycle(self.a), None)