in Programming in C edited by
1,647 views
5 votes
5 votes

Consider the following incomplete C function for reversing a singly linked list.

node* reverse(node* trav){
        if(trav->next)
                __________________
        else 
        {
            head -> next = null;
            head = trav;
        }
        return trav;
}

Here, head is a global pointer pointing to the head of the list and where the head of the reversed list is supposed to be returned. The missing line can be correctly filled by:

  1. reverse(trav->next) -> next = trav;
  2. trav->next -> next = trav;
  3. trav -> next = trav;
  4. trav = reverse(trav->next);
in Programming in C edited by
by
1.6k views

1 comment

Options B and C can't be possible, clearly. They're don't even traverse the whole list, how would they reverse it.

Now make a dummy list and try Options A and D. A wins.
0
0

1 Answer

4 votes
4 votes
Recursive Function to reverse linked list :

node* reverse(node* trav){
        if(trav->next)
        reverse(trav->next) = trav;
        else head = trav;
        return trav;
}

4 Comments

@arjun SIr please confirm ??

After reverse : Last node next ptr should be set to null, somehow right ? 

Either it has be managed inside this code or some other way. correct ?

2
2
yes, you are correct. I corrected it now, else the last node is not pointing to NULL.
2
2
where is code for backtracking
0
0
Answer:

Related questions