题目:
地址:
方法:
我想是先将两个链表逆序,之后再遍历直到找到第一个不同的节点,返回
代码:
class ListNode: def __init__(self, x): self.val = x self.next = None def reverse(pHead): node = ListNode(None) node.next = pHead tmp = pHead while(tmp.next!=None): tmp2 = tmp.next tmp.next = tmp2.next tmp2.next = node.next node.next = tmp2 return node.next # class Solution: def FindFirstCommonNode(self , pHead1 , pHead2 ): if (pHead1==None)or(pHead2==None): return 0; p1 = reverse(pHead1) p2 = reverse(pHead2) while(p1.next==p2.next)and(p1.next!=None): p1 = p1.next p2 = p2.next return p1
结果:出错但是找不到原因,想了很长时间可能是当局者迷,求解!
全部评论
(2) 回帖