AcWing 66. 两个链表的第一个公共结点 - Python3
原题链接
简单
作者:
KYCygni
,
2021-03-31 13:49:09
,
所有人可见
,
阅读 260
Python3 代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def findFirstCommonNode(self, headA, headB):
"""
:type headA, headB: ListNode
:rtype: ListNode
"""
ha = headA
hb = headB
while True:
if ha == hb:
break
if ha is None:
ha = headB
else:
ha = ha.next
if hb is None:
hb = headA
else:
hb = hb.next
return ha