Solutions

1. Direct

先得判断是否有环，分几个情况讨论，如果两个都没有环，就调用无环单链表是否相交的判断方法；如果一个有环、一个没环，说明不可能相交；如果两个都有环就调用有环的判断。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class ChkIntersection:
return None
while fast and slow:
if fast.next:
fast = fast.next.next
else:
return None
slow = slow.next
if fast == slow:
break

if not fast or not slow:
return None

while restart != slow:
restart = restart.next
slow = slow.next
return slow
n1, n2 = 0, 0
while node1 != point1:
node1 = node1.next
n1 += 1
while node2 != point2:
node2 = node2.next
n2 += 1
diff = abs(n1 - n2)
if n1 > n2:
for _ in range(diff):
node1 = node1.next
else:
for _ in range(diff):
node2 = node2.next
while node1 != point1 and node2 != point2:
if node1 == node2:
return node1
node1 = node1.next
node2 = node2.next

node1 = point1
while True:
if node1 == point2:
if n1 > n2:
return point2
else:
return point1
node1 = node1.next
if node1 == point1:
break
return False
# no cycle
return None
size_a, size_b = 0, 0
while nodeA:
size_a += 1
nodeA = nodeA.next
while nodeB:
size_b += 1
nodeB = nodeB.next
if size_a == size_b:
pass
elif size_a > size_b:
steps = size_a - size_b
for _ in range(steps):
nodeA = nodeA.next
else:
steps = size_b - size_a
for _ in range(steps):
nodeB = nodeB.next
while nodeA and nodeB:
if nodeA == nodeB:
return nodeA
nodeA = nodeA.next
nodeB = nodeB.next
return None