本篇文章将分享如何通过 Swift 编写程序,找到两个单链表相交的起始节点。我们将分析问题,提供清晰的题解代码,并通过示例测试验证结果。同时,文章会详细剖析代码逻辑,评估其时间复杂度和空间复杂度,助力开发者掌握高效算法的实现技巧。
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点 c1 开始相交。
示例 1:
输入:intersectVal = 8, listA = 4,1,8,4,5, listB = 5,0,1,8,4,5, skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 4,1,8,4,5,链表 B 为 5,0,1,8,4,5。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
输入:intersectVal = 2, listA = 0,9,1,2,4, listB = 3,2,4, skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 0,9,1,2,4,链表 B 为 3,2,4。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例 3:
输入:intersectVal = 0, listA = 2,6,4, listB = 1,5, skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 2,6,4,链表 B 为 1,5。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。
注意:
null
.以下为 Swift 的题解实现:
class ListNode {
var val: Int
var next: ListNode?
init(_ val: Int) {
self.val = val
self.next = nil
}
}
class Solution {
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
guard headA != nil && headB != nil else { return nil }
var pointerA = headA
var pointerB = headB
while pointerA !== pointerB {
pointerA = pointerA == nil ? headB : pointerA?.next
pointerB = pointerB == nil ? headA : pointerB?.next
}
return pointerA
}
}
headA
和 headB
开始,依次遍历链表。 nil
。 nil
)。 pointerA = pointerA == nil ? headB : pointerA?.next
undefined当 pointerA
到达链表 A 的末尾时,切换到链表 B 的头部,反之亦然。 // 示例链表创建
let common = ListNode(8)
common.next = ListNode(4)
common.next?.next = ListNode(5)
let listA = ListNode(4)
listA.next = ListNode(1)
listA.next?.next = common
let listB = ListNode(5)
listB.next = ListNode(0)
listB.next?.next = ListNode(1)
listB.next?.next?.next = common
// 测试用例
let solution = Solution()
if let intersection = solution.getIntersectionNode(listA, listB) {
print("Intersection Node Value: \(intersection.val)") // 输出:8
} else {
print("No Intersection")
}
输出结果:
Intersection Node Value: 8
通过双指针法,成功实现了寻找链表相交节点的高效算法。其优势在于时间复杂度 (O(n))、空间复杂度 (O(1)) 的表现。实际应用中,该方法具有较高的通用性,非常适合处理单链表相关问题。
未来可以进一步扩展到其他链表问题,例如检测链表环、合并链表等。希望本篇文章能帮助大家更好地理解和掌握链表算法的核心技巧!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。