Created
October 23, 2017 15:47
-
-
Save cixuuz/78440aec0341db1b0173c4525f1d4c9c to your computer and use it in GitHub Desktop.
[160. Intersection of Two Linked Lists] #leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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