简体   繁体   English

在循环链表的末尾插入节点的时间复杂度?

[英]Time complexity to insert a node at the end of circular linked list?

Time complexity to insert a node at the end of circular singly linked list containing N elements ? 在包含N个元素的圆形单链表的末尾插入节点的时间复杂度? Suppose I have pointer to the first node. 假设我有指向第一个节点的指针。


I think that it is O(N) as I have to parse the LL to the node before the new node to modify its next field. 我认为它是O(N),因为我必须在新节点修改其下一个字段之前将LL解析到节点。

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

Adding to the end of a circular singly linked list can be done in O(1) time. 添加到循环单链表的末尾可以在O(1)时间内完成。

  1. Create a new node and insert it after your head node. 创建一个新节点并将其插入头节点之后。
  2. Copy your data from head into this new node. 将数据从头部复制到此新节点。
  3. Add your new data into the old head node. 将新数据添加到旧头节点。
  4. Re-assign head. 重新指定头部。

As these are all constant time operations, this procedure is O(1). 由于这些都是常量时间操作,因此该过程为O(1)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM