Skip to main content

160 - Intersection of Two Linked Lists

Details

KeyValue
Linkhttps://leetcode.com/problems/intersection-of-two-linked-lists/
LanguagePython 3
Runtime271 ms, faster than 19.44% of Python3 online submissions for Intersection of Two Linked Lists
Memory Usage29.5 MB, less than 93.74% of Python3 online submissions for Intersection of Two Linked Lists
DatastructuresLinked List
Algorithms2 pointers

Procedure

  1. TBD...

Code

class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
p1, p2 = headA, headB
while p1 is not p2:
p1 = headB if p1 is None else p1.next
p2 = headA if p2 is None else p2.next
return p1