#defination of ListNode
class ListNode:
def __init__(self,x):
self.val=x
self.next=None
class solution:
def firstComment(self,head1,head2):
temp1,temp2=head1,head2
while temp1.next is not None and temp2.next is not None:
temp1=temp1.next
temp2=temp2.next
if temp1==temp2:
return temp1.val
if temp1.next is None:
temp1,temp2=temp2,head1
elif temp2.next is None:
temp1,temp2=head2,temp1
while temp1 is not None:
if temp1==temp2:
return temp1.val
temp1=temp1.next
temp2=temp2.next
if __name__=="__main__":
head1=ListNode(1)
head1.next=ListNode(2)
head2=ListNode(3)
head1.next.next=head2.next=ListNode(4)
su=solution()
print(su.firstComment(head1,head2))
剑指offer----两个链表的第一个公共节点
猜你喜欢
转载自blog.csdn.net/haoshan4783/article/details/88643239
今日推荐
周排行