-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdailycoding020.py
More file actions
87 lines (66 loc) · 1.79 KB
/
dailycoding020.py
File metadata and controls
87 lines (66 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class ListNode:
def __init__(self, val):
self.next = None
self.val = val
class List:
def __init__(self):
self.head = ListNode(0)
def get_head(self):
return self.head.next
def get_tail(self):
temp = self.head.next
while temp.next:
temp = temp.next
return temp
def get_node(self, index):
temp = self.head.next
for _ in range(index):
temp = temp.next
return temp
def insert(self, val):
temp = self.head
while temp.next:
temp = temp.next
temp.next = ListNode(val)
def traverse(self):
temp, liststr = self.head.next, ""
while temp:
liststr += f"{temp.val} -> " if temp.next else str(temp.val)
temp = temp.next
print(liststr)
def make_loop(head, count=1):
while head.next:
head = head.next
count += 1
return count, head
def find_intersection(list1, list2):
# Find the length of list1 and make it cyclic
count, end = make_loop(list1)
end.next = list1
# advance the runner ptr [count] times
temp, runner = list2, list2
for _ in range(count):
if runner is None:
return None
runner = runner.next
# Loop until equal
while runner != temp:
runner = runner.next
temp = temp.next
return temp
def main():
list1 = List()
list1.insert(1)
list1.insert(2)
list1.insert(3)
list1.insert(4)
list2 = List()
list2.insert(5)
list2.insert(6)
list2.insert(7)
list2.get_tail().next = list1.get_node(2)
l1, l2 = list1.get_head(), list2.get_head()
inter_node = find_intersection(l1, l2)
print(inter_node.val if inter_node else -1)
if __name__ == '__main__':
main()