A first common junction 36. The two lists of (Python)

Title Description

Two input lists, find their first common node.
 1 class Solution:
 2     def FindFirstCommonNode(self, pHead1, pHead2):
 3         # write code here
 4         if pHead1==pHead2:
 5             return pHead1
 6         p1 = pHead1
 7         p2 = pHead2
 8         flag=1
 9         while p1 and p2 !=None:
10             p1 = p1.next
11             p2 = p2.next
12         if p1 == None:
13             flag=2
14              P1 = P2
 15          K = 0
 16          the while P1: # length difference calculating two lists
 . 17              K +. 1 =
 18 is              P1 = p1.next
 . 19          IF In Flag ==. 1 : recording long #flag that list
 20 is              the while K> 0:
 21 is                  pHead1 = pHead1.next
 22 is                  - K-k = go. 1 step #
 23 is          the else :
 24              the while k> 0:
 25                  pHead2 = pHead2.next
 26 is                  - K-=. 1
 27          the while pHead1 and pHead2:
28             if pHead1==pHead2:
29                 return pHead1
30             pHead1=pHead1.next
31             pHead2=pHead2.next
32         return None

2019-12-25 10:39:02

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12095374.html