mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 面试题02.07.链表相交 python3版本
This commit is contained in:
@ -151,7 +151,44 @@ public class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
|
||||
class Solution:
|
||||
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
|
||||
lengthA,lengthB = 0,0
|
||||
curA,curB = headA,headB
|
||||
while(curA!=None): #求链表A的长度
|
||||
curA = curA.next
|
||||
lengthA +=1
|
||||
|
||||
while(curB!=None): #求链表B的长度
|
||||
curB = curB.next
|
||||
lengthB +=1
|
||||
|
||||
curA, curB = headA, headB
|
||||
|
||||
if lengthB>lengthA: #让curA为最长链表的头,lenA为其长度
|
||||
lengthA, lengthB = lengthB, lengthA
|
||||
curA, curB = curB, curA
|
||||
|
||||
gap = lengthA - lengthB #求长度差
|
||||
while(gap!=0):
|
||||
curA = curA.next #让curA和curB在同一起点上
|
||||
gap -= 1
|
||||
|
||||
while(curA!=None):
|
||||
if curA == curB:
|
||||
return curA
|
||||
else:
|
||||
curA = curA.next
|
||||
curB = curB.next
|
||||
return None
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user