leetcode-mid-Linked list-160 Intersection of Two Linked Lists-NO

mycode with the reverse list, it does not meet the meaning of the questions

 

reference:

Ideas:

1 long chain let go and see whether or not encounter the same length

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        if not headA or not headB:
            return None
        def cal(head):
            count = 0        
            while head:
                count += 1
                head = head.next
            return count
        countA = cal(headA)
        countB =CAL (headB) 
        PLUS = countA - countB
         IF PLUS> 0:
             the while PLUS: 
                headA = headA.next 
                PLUS -. 1 = 
            left = countB
         the else : 
            PLUS = ABS (PLUS)
             the while PLUS: 
                headB = headB.next 
                PLUS -. 1 = 
            left = countA
         the while left: # here is whether or headB headA can be friends, because two people have the same pace of friends
            if headA == headB:
                return headA
            headA = headA.next
            headB = headB.next
            left -= 1
        return None
                

2 so that the short list come first, and then away from the head of the list from the long, so that when completed long chain and short chain just a few steps down the length of the difference in the long chain, the chain length further from the short list head start to go, equivalent to the same starting line two friends

class Solution (Object):
     DEF getIntersectionNode (Self, headA, headB): 
 
        IF  Not headA or  Not headB:
             return None 
   
        p, q = headA, headB    
 
        the while p = q:!    # executed when p is not equal to q program below 
            p = headB IF p iS none the else p1.next    # if p is not none, to remove a value is nONE = headB let p 
            q = headA IF q iS none the else q.next     # If q is not none, to remove a value is NONE = headA let Q 
return            
        P1    # P, Q are equal there are two cases, one is intersected, the output point of intersection A is disjoint, output NONE

 

Guess you like

Origin www.cnblogs.com/rosyYY/p/10967372.html