从链表中的最后一个元素有效地找到第n个 [英] Efficiently finding nth from last element in a linked list
问题描述
推荐答案
实际上,链接到列表不是用于此目的的最佳数据结构(例如通过索引查找指定的元素)。当然,您可以使用一些小技巧,例如存储桶的列表(有限大小的列表)和从两端搜索元素。它可能会增加一点点,但没有那么有效(平均复杂度将保持为O(N))此外,它将是复杂的结构。我认为任何方法将导致创建指定类型的类型(如数组或字典)。如果元素访问的复杂性是至关重要的,那么数组的使用可以帮助吗?如果您经常在列表末尾添加元素,则可以使用具有摊销能力的数组
Actually, linked list is not best data structure for such purposes (like finding specified element via index). Of course, you can use some little tricks, like storing list of buckets (lists with limited size) and searching element from two ends. It may boost finding little bit, but no so effectively (average complexity will stay O(N)) Moreover it will complex structure. I think that any method will lead to creation of type of specified kind (like array or dictionary). If complexity of element's access is critical then maybe usage of array can help? If you often adds elements in the end of list you can use array with amortized capacity increasing
这篇关于从链表中的最后一个元素有效地找到第n个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!