架构师训练营第八周作业 1
寻找两个 linked list 的交叉 node
Definition for singly-linked list.
class ListNode:
def _init_(self, x):
self.val = x
self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
p1 = headA
p2 = headB
while p1!=p2:
if p1 is None:
p1 = headB
else:
p1 = p1.next
if p2 is None:
p2 = headA
else:
p2 = p2.next
return p1
时间复杂度 O(m+n), 空间复杂度 O(1)
评论