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
    }
}
相关推荐
一点晖光1 小时前
ios底部按钮被挡住
前端·ios·微信小程序
长安er8 小时前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓8 小时前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
小白菜又菜8 小时前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
粉红色回忆9 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++
予枫的编程笔记10 小时前
Redis 核心数据结构深度解密:从基础命令到源码架构
java·数据结构·数据库·redis·缓存·架构
wadesir10 小时前
掌握Rust并发数据结构(从零开始构建线程安全的多线程应用)
数据结构·安全·rust
chinesegf11 小时前
iTunes Lookup API 规则具体(查包名)
ios
charliejohn11 小时前
计算机考研 408 数据结构 哈夫曼
数据结构·考研·算法
POLITE312 小时前
Leetcode 41.缺失的第一个正数 JavaScript (Day 7)
javascript·算法·leetcode