Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 23, 2017 15:47
Show Gist options
  • Save cixuuz/78440aec0341db1b0173c4525f1d4c9c to your computer and use it in GitHub Desktop.
Save cixuuz/78440aec0341db1b0173c4525f1d4c9c to your computer and use it in GitHub Desktop.
[160. Intersection of Two Linked Lists] #leetcode
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
# corner case if any list is empty
if headA is None or headB is None:
return None
# initialize end nodes and head nodes
pA = headA
pB = headB
while pA is not pB:
pA = headB if pA is None else pA.next
pB = headA if pB is None else pB.next
return pA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment