简体   繁体   English

在n个节点的循环单列表中某个指针指向的节点之前插入一个节点的时间复杂度?

[英]Time complexity to insert a node before the node pointed by some pointer in circular singly list of n nodes?

I think that it is O(N) because in a circular singly list the pointer visits n nodes so that node to be inserted precedes the current node.我认为它是 O(N) ,因为在循环单个列表中,指针访问 n 个节点,以便要插入的节点在当前节点之前。

Have I got it right?我做对了吗?

The usual traversal and insertion would take O(N) time.通常的遍历和插入将花费O(N)时间。 However, it can be done in O(1) as well, if the nodes can be modified.但是,如果可以修改节点,也可以在O(1)中完成。
Let the list look like A -> B-> C -> D -> A , we are given a pointer to C , and we have to insert E .让列表看起来像A -> B-> C -> D -> A ,我们得到一个指向C的指针,我们必须插入E Now, create a copy of C and insert it after C itself.现在,创建C的副本并将其插入到C本身之后。 The list now looks like A -> B-> C -> C(copy) -> D -> A .该列表现在看起来像A -> B-> C -> C(copy) -> D -> A Now, we can modify the values of the original C node to store the values of E node, so the list now becomes A -> B-> E -> C -> D -> A .现在,我们可以修改原来的C节点的值来存储E节点的值,所以列表现在变为A -> B-> E -> C -> D -> A Since you've only created a node and inserted it in place, it takes O(1) time complexity.由于您只创建了一个节点并将其插入到位,因此需要O(1)时间复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单链表中查找节点的时间复杂度 - Finding a node in singly linked list time complexity 在循环链表的末尾插入节点的时间复杂度? - Time complexity to insert a node at the end of circular linked list? 将新节点插入具有n个节点的最低级别的BST的最佳情况的时间复杂度是多少? - What is the time complexity of the best case to insert a new node into a minimum-level BST with n nodes? 如何将新节点插入单链表,我们没有指向其头部的指针? - How to insert a new node to a single-linked list, where we do not have a pointer pointed to its head? 删除单链表中的节点 - Remove node in a singly linked list 在排序的链表中插入节点的时间复杂性 - Time complexity of inserting a node in a sorted linked list 如何在新的单向链表中返回单向链表的奇数索引节点?假设第一个节点的索引为 1 - How can I return the odd indexed nodes of a singly linked list in a new singly linked list ?Assume index of the first node as 1 如何在O(n)时间内以某个给定大小的块反转单链表? - How to reverse a singly-linked list in blocks of some given size in O(n) time in place? 实现一种算法,将节点插入循环链表而不遍历它 - Implement an algorithm to insert a node into a circular linked list without traversing it 将节点移动到单链表的开头 - Move node to beginning of singly linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM