Skip to content

Commit 4abc917

Browse files
committedDec 13, 2020
feat: intersection-of-two-linked-lists
1 parent a98be0d commit 4abc917

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

Diff for: ‎list.intersection-of-two-linked-lists.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution:
9+
"""
10+
160. 相交链表
11+
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
12+
编写一个程序,找到两个单链表相交的起始节点。
13+
"""
14+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
15+
if not headA or not headB:
16+
return None
17+
18+
pA, pB = headA, headB
19+
while pA != pB:
20+
pA = headB if pA is None else pA.next
21+
pB = headA if pB is None else pB.next
22+
23+
return pA
24+
25+

0 commit comments

Comments
 (0)
Please sign in to comment.