File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -151,7 +151,44 @@ public class Solution {
151
151
```
152
152
153
153
Python:
154
-
154
+ ``` python
155
+ # Definition for singly-linked list.
156
+ # class ListNode:
157
+ # def __init__(self, x):
158
+ # self.val = x
159
+ # self.next = None
160
+
161
+ class Solution :
162
+ def getIntersectionNode (self , headA : ListNode, headB : ListNode) -> ListNode:
163
+ lengthA,lengthB = 0 ,0
164
+ curA,curB = headA,headB
165
+ while (curA!= None ): # 求链表A的长度
166
+ curA = curA.next
167
+ lengthA += 1
168
+
169
+ while (curB!= None ): # 求链表B的长度
170
+ curB = curB.next
171
+ lengthB += 1
172
+
173
+ curA, curB = headA, headB
174
+
175
+ if lengthB> lengthA: # 让curA为最长链表的头,lenA为其长度
176
+ lengthA, lengthB = lengthB, lengthA
177
+ curA, curB = curB, curA
178
+
179
+ gap = lengthA - lengthB # 求长度差
180
+ while (gap!= 0 ):
181
+ curA = curA.next # 让curA和curB在同一起点上
182
+ gap -= 1
183
+
184
+ while (curA!= None ):
185
+ if curA == curB:
186
+ return curA
187
+ else :
188
+ curA = curA.next
189
+ curB = curB.next
190
+ return None
191
+ ```
155
192
156
193
Go:
157
194
You can’t perform that action at this time.
0 commit comments