Swift solution


  • 0
    class Solution {
        func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
            if headA == nil || headB == nil {
                return nil
            }
            
            var a: ListNode? = headA
            var b: ListNode? = headB
            
            while a !== b {
                a = a == nil ? headB : a?.next
                b = b == nil ? headA : b?.next
            }
            
            return a;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.