160. 相交链表 (Swift版本)

题目描述

最简单直接的解法

遍历 headA 的所有节点, 看 headB 中是否有相交的节点

swift 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */

class Solution {
 
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
    var next: ListNode? = headA
    while next != nil {
        if containNode(headB, next) {
            return next
        }
        next = next?.next
    }
    return nil
}

func containNode(_ headB: ListNode?, _ node: ListNode?) -> Bool {
    var next: ListNode? = headB
    while next != nil {
        if node === next { return true }
        next = next?.next
    }
    return false
}
}

哈希集合优化

因为示例中的 ListNode 没有实现 Hash 相关协议, 所以无法使用 Set, 这里使用 Array 代替. (LeetCode, 咱能不能重视一下 Swift 用户, OK ?)

swift 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */

class Solution {


var globalSet = Array<ListNode?>()
 
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
    var next: ListNode? = headA

    if (globalSet.contains { $0 === next }) {
        return next
    }

    while next != nil {
        if containNode(headB, next) {
            return next
        }
        next = next?.next
    }
    return nil
}

func containNode(_ headB: ListNode?, _ node: ListNode?) -> Bool {

    var next: ListNode? = headB

    while next != nil {
        if node === next { return true }
        if (!globalSet.contains { $0 === next }) {
            globalSet.append(next)
        }
        next = next?.next
    }
    return false
}

}

上面两个方法提交后, 结果都是: 超出时间限制

双指针

swift 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */

class Solution {
    func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
        if headA == nil || headB == nil {
            return nil
        }
        var pA = headA, pB = headB
        while pA !== pB {
            pA = pA == nil ? headB : pA?.next
            pB = pB == nil ? headA : pB?.next
        }
        return pA
    }
}
相关推荐
岁忧8 分钟前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油8 分钟前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
月盈缺3 小时前
学习嵌入式的第二十二天——数据结构——双向链表
数据结构·学习·链表
HarderCoder4 小时前
我们真的需要 typealias 吗?——一次 Swift 抽象成本的深度剖析
swift
科大饭桶4 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
HarderCoder5 小时前
ByAI-Swift 6 全览:一份面向实战开发者的新特性速查手册
swift
元亓亓亓5 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
躲在云朵里`5 小时前
深入理解数据结构:从数组、链表到B树家族
数据结构·b树
HarderCoder6 小时前
Swift 中 let 与 var 的真正区别:不仅关乎“可变”与否
swift
HarderCoder6 小时前
深入理解 Swift 6.2 并发:从默认隔离到@concurrent 的完整指南
swift