• Intersection of Two Linked Lists


     public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
            
           var pointA = headA;
            var pointB = headB;
            if(headA != null && headB != null) {
                while(pointA != pointB) {
                    if(pointA == null) {
                        pointA = headB;
                    } else {
                        pointA = pointA.next;
                    }
                    if(pointB == null) {
                        pointB = headA;
                    } else {
                        pointB = pointB.next;
                    }
                    if((pointA == null) && (pointB == null)) {
                        return null;
                    }
                }
                return pointA;
            } else {
                return null;
            }
     
        }
    或者 等同于
    if (headA == null || headB == null)
                    return null;
                ListNode currA = headA;
                ListNode currB = headB;
                while (currA!=currB)
                {
                    currA = currA == null ? headB : currA.next;
                    currB = currB == null ? headA : currB.next;
                }
                return currA;
     private static ListNode GetIntersectionNode2(ListNode headA, ListNode headB) {
            var nodeA = headA;
            var nodeB = headB;
            var set = new HashSet<ListNode>();
            while(nodeA != null) {
                set.Add(nodeA);
                nodeA = nodeA.next;
            }
            while(nodeB != null) {
                if(set.Contains(nodeB)) {
                    return nodeB;
                }
                nodeB = nodeB.next;
            }
            return null;
        }
    
     
  • 相关阅读:
    Codeforces Round #319 (Div. 2) D
    因为网络请求是 异步的,
    ios真蛋疼,
    单例模式的两种实现,
    jump, jump,
    一点 误删,
    关于代理,
    button上的两个手势,
    数据,
    header 的蓝色,
  • 原文地址:https://www.cnblogs.com/wangcl-8645/p/11215162.html
Copyright © 2020-2023  润新知