Saturday, May 26, 2007

How to read a singly linked list backwards?

Use Recursion.

15 comments:

Anonymous said...

This is no possible in case of singale linked list because we don't have back address

Belamge, Pundaleek said...

One way to do this is using recursive operation

Other way is through LIFO data structure

Anonymous said...

Ya I agree, we can use Stack, and push the elements in stack. and after the POP it till the stack is empty.

Anonymous said...

Can we not write recursive program like

void PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(Root->next)
PrintListFromBack(Root->Next);

printf("%d", Root->info);
}

Anonymous said...

1] Stack solution
2] Recursive function
3] Reverse list and print

Anonymous said...

build another list.

Lakhvir Singh said...

//pass head as the argument
int print_reverse(list *ptr){
if(ptr==NULL)
return 0;
else{
print_reverse(ptr->link);
printf(" %d ",ptr->data);
}
return 0;
}

Anonymous said...

first reverse the list and read

Anonymous said...

algorithm to reverse a singly linked list:

top=null
while(head!=null)
{
temp = start
start = start.next
temp.next = top
top = temp
}

correct, no?

Anonymous said...

CORRECT SOLUTION IS

void PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(Root->next)
PrintListFromBack(Root->Next);

printf("%d", Root->info);
}

Anonymous said...

Copied from cracktheinterview. word to word

Anonymous said...

The solution given with the function PrintListFromBack is a good one and make sense.

Priyanka kumari said...

one way void readfrmbck(struct node *start)
{
if(start==NULL)
return;

//printf("%d",start->data);
readfrmbck(start->next);
printf("%d",start->data);
}to solve this question is:

bunny said...

one way to solve this is:
void readfrmbck(struct node *start)
{
if(start==NULL)
return;

//printf("%d",start->data);
readfrmbck(start->next);
printf("%d",start->data);
}

Namita Agrawal said...

All Government jobs Banks , PSU,CPSU,PSC,UPSC,Defence, Exam,Admit cards,India & Others

All Government jobs Banks , PSU,CPSU,PSC,UPSC,Defence & Others or Submit & verify Email for Latest Jobs Alerts

All Examinations Latest updates or Submit & verify Email for Latest Exams Alerts

Examination Planning & strategy or Submit & verify Email for Latest Exam Plan Alerts

Check your Exam results or Submit & verify Email for Latest Exam Results Alerts

Downloads Your Admit card / Hall Ticket or Submit & verify Email for Latest Admit Cards Alerts