160. 相交链表

image.png

  1. package main
  2. import "unsafe"
  3. type ListNode struct {
  4. Val int
  5. Next *ListNode
  6. }
  7. func getIntersectionNode(headA, headB *ListNode) *ListNode {
  8. m :=make(map[uintptr]struct{})
  9. for headA!=nil||headB!=nil{
  10. if headA!=nil {
  11. if _,ok:=m[uintptr(unsafe.Pointer(headA))];ok{
  12. return headA
  13. }else {
  14. m[uintptr(unsafe.Pointer(headA))] = struct{}{}
  15. }
  16. headA = headA.Next
  17. }
  18. if headB!=nil {
  19. if _,ok:=m[uintptr(unsafe.Pointer(headB))];ok{
  20. return headB
  21. }else {
  22. m[uintptr(unsafe.Pointer(headB))] = struct{}{}
  23. }
  24. headB= headB.Next
  25. }
  26. }
  27. return nil
  28. }
  29. func main() {
  30. }

image.png