예제 #1
0
 def test1(self):
     for f in functions:
         a = list_to_linkedlist([7, 2, 4, 3])
         b = list_to_linkedlist([5, 6, 4])
         node = f(a, b)
         self.assertEqual(linkedlist_to_list(node), [7, 8, 0, 7],
                          f.__name__)
 def test(self):
     for f in functions:
         l1 = list_to_linkedlist([2,3,5])
         l2 = list_to_linkedlist([1,3,6])
         merged = f([l1, l2])
         self.assertEqual(linkedlist_to_list(merged), [1,2,3,3,5,6], f.__name__)
 def test1(self):
     for f in functions:
         node1 = list_to_linkedlist([1, 2, 3, 4, 5])
         node2 = list_to_linkedlist([1, 2, 3, 4, 5, 6])
         node2.next.next.next = node1.next.next
         self.assertEqual(f(node1, node2).val, 3, f.__name__)
예제 #4
0
 def test1(self):
     for f in functions:
         node = list_to_linkedlist([1,2,2,1])
         self.assertEqual(f(node), True, f.__name__)
         self.assertEqual(linkedlist_to_list(node), [1,2,2,1], f.__name__)
예제 #5
0
 def test1(self):
     for f in functions:
         a = list_to_linkedlist([2, 3, 5])
         b = list_to_linkedlist([1, 4, 6])
         self.assertEqual(linkedlist_to_list(f(a, b)), [1, 2, 3, 4, 5, 6],
                          f.__name__)
    # k = s + m
    # s = n*r - m
    # s = (n-1)*r + (r-m)
    # i.e. steps from start to cycle entry = steps from meet to cycle entry(r-m)
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        hasCycle = False
        start = slow = fast = head
        while fast and fast.next and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                hasCycle = True
                break
        if not hasCycle:
            return None
        while start != slow:
            start = start.next
            slow = slow.next
        return start


# test
node = list_to_linkedlist([1, 2, 3, 4])
print(Solution().detectCycle(node))
# link node 4 to node 3
node.next.next.next.next = node.next.next
print(Solution().detectCycle(node).val)
# link node 1 to itself
node.next = node
print(Solution().detectCycle(node).val)
예제 #7
0
 def test1(self):
     for f in functions:
         node = list_to_linkedlist([1, 2, 3, 3, 3, 4, 4, 5])
         self.assertEqual(linkedlist_to_list(f(node)), [1, 2, 3, 4, 5],
                          f.__name__)
예제 #8
0
 def test1(self):
     for f in functions:
         a = list_to_linkedlist([2, 4, 3])
         b = list_to_linkedlist([5, 6, 4])
         self.assertEqual(linkedlist_to_list(f(a, b)), [7, 0, 8],
                          f.__name__)
 def test2(self):
     for f in functions:
         node = list_to_linkedlist([1, 2, 3, 4])
         node.next.next.next.next = node.next.next
         self.assertEqual(f(node), True, f.__name__)
예제 #10
0
 def test1(self):
     for f in functions:
         node = list_to_linkedlist([1, 2, 3, 4])
         self.assertEqual(f(node), False, f.__name__)
 def test1(self):
     for f in functions:
         self.assertEqual(
             linkedlist_to_list(f(list_to_linkedlist([1, 2, 3, 4, 5]))),
             [5, 4, 3, 2, 1], f.__name__)