b ?

The Gateway to Computer Science Excellence

+1 vote

+1 vote

**O(n).**

If it is the Last node of the linked list it will take ** O(n)** time. Despite node pointer is given but we don't know the address of the previous node. From head, we need to traverse the linked list to get the add of the previous node.

If we have address of prevoius node then only we can store the address of the next node that has to be deleted.

**Previous -> next = Current -> next;**

+1

If it is the 2nd last node, it can be deleted in O(1). This is how ->

1. copy data of last node, into 2nd last node

2. Now delete the last node (note we have pointer to previous of this node. i.e. we have pointer to 2nd last node)

However if the original node to be deleted is itself the last node, then even above method can't be appplied and in that case time complexity is O(n). Ans is O(n) bcoz of last node and not because of 2nd last node.

1. copy data of last node, into 2nd last node

2. Now delete the last node (note we have pointer to previous of this node. i.e. we have pointer to 2nd last node)

However if the original node to be deleted is itself the last node, then even above method can't be appplied and in that case time complexity is O(n). Ans is O(n) bcoz of last node and not because of 2nd last node.

52,345 questions

60,517 answers

201,939 comments

95,368 users