题解 | #从单向链表中删除指定值的节点#

两个链表的第一个公共结点

http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

简单方法

class Solution: def FindFirstCommonNode(self , pHead1 , pHead2 ):

    tmp_Node=pHead1
    num1,num2=0,0
    while tmp_Node:
        num1+=1
        tmp_Node=tmp_Node.next
    tmp_Node=pHead2
    while tmp_Node:
        num2+=1
        tmp_Node=tmp_Node.next
    
    if num1<num2:#链表1短
        chazhi=num2-num1
        while chazhi:
            pHead2=pHead2.next
            chazhi-=1
    elif num1>num2:
        chazhi=num1-num2
        while chazhi:
            pHead1=pHead1.next
            chazhi-=1
    
    while pHead1 and pHead2:
        if pHead1==pHead2:
            return pHead1
        else:
            pHead1=pHead1.next
            pHead2=pHead2.next
    
        
    
    
    
    
    
    
   
全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务