<filename>0160.Intersection of Two Linked Lists/solution.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
*****************************************
Author:         zhlinh
Email:          <EMAIL>
Version:        0.0.1
Created Time:   2016-03-21
Last_modify:    2016-03-21
******************************************
'''

'''
Write a program to find the node at which
the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗
B:     b1 → b2 → b3
begin to intersect at node c1.


Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
'''

# 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
        """
        if not headA or not headB:
            return None
        h1, h2 = headA, headB
        while h1 and h2 and h1 != h2:
            h1 = h1.next
            h2 = h2.next
            if h1 == h2:
                return h1
            if not h1:
                h1 = headB
            if not h2:
                h2 = headA
        return h1

<filename>0160.Intersection of Two Linked Lists/solution.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
*****************************************
Author:         zhlinh
Email:          <EMAIL>
Version:        0.0.1
Created Time:   2016-03-21
Last_modify:    2016-03-21
******************************************
'''

'''
Write a program to find the node at which
the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗
B:     b1 → b2 → b3
begin to intersect at node c1.


Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
'''

# 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
        """
        if not headA or not headB:
            return None
        h1, h2 = headA, headB
        while h1 and h2 and h1 != h2:
            h1 = h1.next
            h2 = h2.next
            if h1 == h2:
                return h1
            if not h1:
                h1 = headB
            if not h2:
                h2 = headA
        return h1

#!/usr/bin/env python # -*- coding: utf-8 -*- ***************************************** Author:         zhlinh Email:          <EMAIL> Version:        0.0.1 Created Time:   2016-03-21 Last_modify:    2016-03-21 ****************************************** Write a program to find the node at which the intersection of two singly linked lists begins.   For example, the following two linked lists:  A:          a1 → a2                    ↘                      c1 → c2 → c3                    ↗ B:     b1 → b2 → b3 begin to intersect at node c1.   Notes:  If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. You may assume there are no cycles anywhere in the entire linked structure. Your code should preferably run in O(n) time and use only O(1) memory. Credits: Special thanks to @stellari for adding this problem and creating all test cases. # Definition for singly-linked list. :type head1, head1: ListNode         :rtype: ListNode