The Gateway to Computer Science Excellence

0 votes

The time and space complexity of the most efficient algorithm designed to find the kth node from the end of a linked list which has n nodes is

- O(1), O(1)
- O(n), O(1)
- O(n), O(n)
- O(n2), O(1)

The answer given is d.

Why is it not option b ??

To check kth node from end, its enough to have a slow pointer which starts after kth position. By the time last node is reached, the slow pointer will be in the kth node from end. So its only O(n).

0

**my approach**(please see if it is correct or not)

algo

1. first find N by traversing the list till end and incrementing a counter(n) for each node. -> O(n)

2. then do N-k+1 to find the node to be found from starting of linked list. -> O(1)

3. then again traverse till the desired node from starting of linked list. -> O(n).

so T.C = O(n) + O(n) = O(n)

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

50,666 questions

56,159 answers

193,768 comments

93,766 users