Consider the problem of reversing a singly linked list. To take an example, given the linked list below, the reversed linked list should look like Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space?

Consider the problem of reversing a singly linked list. To take an example, given the linked list below, the reversed linked list should look like Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space? Correct Answer The best algorithm for the problem takes <span style="">θ(n)</span> time in the worst case. 

The correct answer is option 1.

Concept:

Single linked list:

A single linked list is a sort of unidirectional linked list that may only be traversed in one way, from head to final node (tail). A node is a name for each element in a linked list. A single node includes data as well as a pointer to the next node, which aids in the list's structure.

Explanation:

The time complexity of reversing a linked list is linear in time i.e O(n) where n is the size of the linked list.

Algorithm:

  1. Initialize three-pointers prev as NULL, curr as head and next as NULL.
  2. Iterate through the linked list. In loop, do following. 
    // Before changing next of current, store next node 
    next = curr->next
    // Now change next of current This is where actual reversing happens 
    curr->next = prev 
    // Move prev and curr one step forward 
    prev = curr 
    curr = next

Analysis:

  • Initialize three-pointers prev as NULL, curr as head and next as NULL.
  • Iterate through the linked list,
  • And check whether there are any nodes in the linked list then move forward to the nodes by changing the nodes allocations.
  •  Before changing the next of current, store the next node.
  • Now change next of current This is where actual reversing happens with curr->next = prev.
  • Move prev and curr one step forward with, prev = curr  and curr = next.

So we need to traverse the whole linked list in the given linked list it takes n time to travel all elements in the linked list. So the best algorithm for the problem takes θ(n) time in the worst case. 

Hence the correct answer is the best algorithm for the problem that takes θ(n) time in the worst case. 

Related Questions

Consider a singly linked list of the form where F is a pointer to the first element in the linked list and L is the pointer to the last element in the list. The time of which of the following operations depends on the length of the list?
The following are the conditions for selecting list of a suitable candidates to be called for interview after the written test for the recruitment is conducted/ organized for management-level persons of a multi-national company. For providing accounting services and sales the candidates must (a) be holding a graduation in basic science with 65% or above or B. E degree with 55% and above marks (b) have passed the written test with 70% or above marks (c) the age must be in the group 25 to 30 years as on 1/4/18 (d) have experience in an accounting firms for three years and diploma in accounting with 60% or above marks (e) be presently drawing CTC of 6 Lakhs per annum and above In case the applicant who satisfies all other terms above except 1) at (a) above, then be referred as Junior Accountant 2) at (d) & (e) above then be referred as Trainee-Accountant Satisfying all the above with experience of 5 years then be referred as senior-Accountant Satisfying all the above criteria (a-e) with CA/ ICWA / MBA (Finance) then be refereed as manager (Accounts) Read all the above information and answer the following question Shravani has passed H. SC with 72% of marks. She has done diploma in accountancy with 62% of marks. She was working with an organization in the field of accounting in the field of accounting from 4 years and was drawing CTC of 6.5 Lakhs presently. She is 28yrs as on July 2018. She may referred for the position of: