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
    }
}
相关推荐
HarderCoder38 分钟前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
叽哥11 小时前
Flutter Riverpod上手指南
android·flutter·ios
HarderCoder1 天前
Swift 6 并发时代,如何优雅地“抢救”你的单例?
swift
zhangmeng1 天前
FlutterBoost在iOS26真机运行崩溃问题
flutter·app·swift
HarderCoder1 天前
SwiftUI 踩坑记:onAppear / task 不回调?90% 撞上了“空壳视图”!
swift
HarderCoder1 天前
@isolated(any) 深度解析:Swift 并发中的“隔离追踪器”
swift
大熊猫侯佩1 天前
桃花岛 Xcode 构建秘籍:Swift 中的 “Feature Flags” 心法
app·xcode·swift
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
HarderCoder2 天前
Swift 6.2 新特性 `@concurrent` 完全导读
swift