b ?

The Gateway to Computer Science Excellence

+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.

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.5k
- Others 1.5k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,648 questions

56,430 answers

195,211 comments

99,927 users