|
6 | 6 | from test_framework.test_failure import TestFailure
|
7 | 7 | from test_framework.test_utils import enable_executor_hook
|
8 | 8 |
|
| 9 | +""" |
| 10 | +Cases: |
| 11 | + - No cycle, no overlap |
| 12 | + - Cycle, no overlap |
| 13 | + - No cycle, overlap |
| 14 | + - Cycle, overlap |
9 | 15 |
|
10 |
| -def overlapping_lists(l0: ListNode, l1: ListNode) -> Optional[ListNode]: |
11 |
| - # TODO - you fill in here. |
| 16 | +if only one is cyclic: |
| 17 | + return no overlap |
| 18 | +if both are cyclic: |
| 19 | + get beginning of cycle for both. Starting at one, |
| 20 | + if it wraps all the way around without reaching the other's beginning |
| 21 | + of cycle, no overlap |
| 22 | +else: |
| 23 | + shorten to equal length, use |
| 24 | + previous method |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +def get_list_len(head: ListNode): |
| 29 | + fast = slow = head |
| 30 | + i = 0 |
| 31 | + while slow: |
| 32 | + slow = slow.next |
| 33 | + i += 1 |
| 34 | + if fast: |
| 35 | + fast = fast.next |
| 36 | + if fast: |
| 37 | + fast = fast.next |
| 38 | + if fast and fast is slow: |
| 39 | + return -1 |
| 40 | + return i |
| 41 | + |
| 42 | + |
| 43 | +def check_noncyclic_overlap(l0, l0_len, l1, l1_len): |
| 44 | + while l0_len > l1_len: |
| 45 | + l0 = l0.next |
| 46 | + l0_len -= 1 |
| 47 | + |
| 48 | + while l1_len > l0_len: |
| 49 | + l1 = l1.next |
| 50 | + l1_len -= 1 |
| 51 | + |
| 52 | + while l0 is not l1: |
| 53 | + l0 = l0.next |
| 54 | + l1 = l1.next |
| 55 | + |
| 56 | + return l0 |
| 57 | + |
| 58 | + |
| 59 | +def get_cycle_head(head: ListNode): |
| 60 | + slow = fast = head |
| 61 | + while True: |
| 62 | + slow = slow.next |
| 63 | + fast = fast.next.next |
| 64 | + if slow is fast: |
| 65 | + break |
| 66 | + |
| 67 | + slow = head |
| 68 | + while slow is not fast: |
| 69 | + slow = slow.next |
| 70 | + fast = fast.next |
| 71 | + |
| 72 | + return fast |
| 73 | + |
| 74 | + |
| 75 | +def check_cyclic_overlap(l0, l1): |
| 76 | + l0_cycle_head = get_cycle_head(l0) |
| 77 | + l1_cycle_head = get_cycle_head(l1) |
| 78 | + if l0_cycle_head is l1_cycle_head: |
| 79 | + return l0_cycle_head |
| 80 | + |
| 81 | + curr = l0_cycle_head.next |
| 82 | + while curr is not l0_cycle_head: |
| 83 | + if curr is l1_cycle_head: |
| 84 | + return curr |
| 85 | + curr = curr.next |
12 | 86 | return None
|
13 | 87 |
|
14 | 88 |
|
| 89 | +def overlapping_lists(l0: ListNode, l1: ListNode) -> Optional[ListNode]: |
| 90 | + if not l0 or not l1: |
| 91 | + return None |
| 92 | + l0_len = get_list_len(l0) |
| 93 | + l1_len = get_list_len(l1) |
| 94 | + |
| 95 | + # One's a cycle, the other is not |
| 96 | + if -1 in [l0_len, l1_len] and l0_len != l1_len: |
| 97 | + return None |
| 98 | + |
| 99 | + # Neither is cyclic |
| 100 | + if l0_len != -1: |
| 101 | + return check_noncyclic_overlap(l0, l0_len, l1, l1_len) |
| 102 | + else: |
| 103 | + return check_cyclic_overlap(l0, l1) |
| 104 | + |
| 105 | + |
15 | 106 | @enable_executor_hook
|
16 | 107 | def overlapping_lists_wrapper(executor, l0, l1, common, cycle0, cycle1):
|
17 | 108 | if common:
|
@@ -65,7 +156,26 @@ def overlapping_lists_wrapper(executor, l0, l1, common, cycle0, cycle1):
|
65 | 156 | raise TestFailure('Invalid result')
|
66 | 157 |
|
67 | 158 |
|
| 159 | +def print_list(head): |
| 160 | + seen = set() |
| 161 | + while head: |
| 162 | + if id(head) in seen: |
| 163 | + print("cycle detected; quitting.") |
| 164 | + return |
| 165 | + seen.add(id(head)) |
| 166 | + print(f"node: {head.data}") |
| 167 | + head = head.next |
| 168 | + |
| 169 | + |
68 | 170 | if __name__ == '__main__':
|
| 171 | + """ |
| 172 | + head = curr = ListNode(0, None) |
| 173 | + for i in range(1, 10): |
| 174 | + curr.next = ListNode(i, None) |
| 175 | + curr = curr.next |
| 176 | + # curr.next = head.next.next.next |
| 177 | + print(overlapping_lists(head, head.next.next.next.next).data) |
| 178 | + """ |
69 | 179 | exit(
|
70 | 180 | generic_test.generic_test_main('do_lists_overlap.py',
|
71 | 181 | 'do_lists_overlap.tsv',
|
|
0 commit comments