go解题答案

  • 思路概括:map查找优化
  • 思路核心:
    1、遍历第二个链表去匹配第一个
    2、用map优化查找速度
    func FindFirstCommonNode( pHead1 *ListNode ,  pHead2 *ListNode ) *ListNode {
      // write code here
      h:=map[int]*ListNode{}
      cur :=pHead1
      for cur !=nil{
          h[cur.Val] = cur
          cur= cur.Next
      }
      cur2:=pHead2
      for cur2!=nil{
          if h[cur2.Val]==cur2{
              return cur2
          }
          cur2= cur2.Next
      }
      return nil
    }

如果有帮助请点个赞哦, 更多文章请看我的博客

题主背景

  • 从业8年——超级内卷500Q技术经理——目前专注go和微服务架构