in DS edited by
25,150 views
0 votes
0 votes

In a linked list with $n$ nodes, the time taken to insert an element after an element
pointed by some pointer is:

$(A) O(1)$
$(B) O(logn)$
$(C) O(n)$
$(D) O(nlogn)$

in DS edited by
25.2k views

1 Answer

1 vote
1 vote
To insert an element after an element pointed by some pointer in a linked list will take constant time (only the next pointers of the newly inserted node and the node pointed to by the pointer needs to be changed). So ans is option (a) O(1).

4 Comments

@Verma Ashish yes it will take constant time
0
0

@Verma Ashish 

see my above comment.

Yeah, it takes $O(1)$ times because already some pointer pointed to the element.(If some pointer does not point the element and we want to insert the element at the end of the linked list(tail sometimes it is called rear of the linked list) , it takes $O(n)$, because we traverse the entire list, from the start node(head, sometimes it is called front of the linked list)

 

0
0
Let the pointer pointing to element be x
now we create a new node y
now to insert y we point next pointer of y to the node pointed by next pointer of x
and then point the next pointer of x to y....we are done inserting the element
0
0