牛客剑指offer:两个链表的第一个公共节点为什么过不去?
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
# write code here
if not pHead1 or not pHead2:
return
dummy1 = pHead1
dummy2 = pHead2
while dummy1 != dummy2:
if dummy1.next:
dummy1 = dummy1.next
else:
dummy1 = pHead2
if dummy2.next:
dummy2 = dummy2.next
else:
dummy2 = pHead1
return dummy1
感觉代码逻辑没问题,就是超时,ac为0,求助。思路是当一个链表的指针走到尾节点,下次移动就将其指向另一个链表的头结点,最终两个指针会在公共节点相遇。#笔试题目##Python#