Please post your answer in comments if you have a better solution.
This is no possible in case of singale linked list because we don't have back address
One way to do this is using recursive operationOther way is through LIFO data structure
Ya I agree, we can use Stack, and push the elements in stack. and after the POP it till the stack is empty.
Can we not write recursive program likevoid PrintListFromBack(LIST * Root){ if(!Root) return; else if(Root->next) PrintListFromBack(Root->Next); printf("%d", Root->info);}
1] Stack solution2] Recursive function3] Reverse list and print
build another list.
//pass head as the argumentint print_reverse(list *ptr){ if(ptr==NULL) return 0; else{ print_reverse(ptr->link); printf(" %d ",ptr->data); } return 0;}
first reverse the list and read
algorithm to reverse a singly linked list:top=nullwhile(head!=null){temp = startstart = start.nexttemp.next = toptop = temp}correct, no?
CORRECT SOLUTION ISvoid PrintListFromBack(LIST * Root){if(!Root)return;elseif(Root->next)PrintListFromBack(Root->Next);printf("%d", Root->info);}
Copied from cracktheinterview. word to word
The solution given with the function PrintListFromBack is a good one and make sense.
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:
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);}
This is no possible in case of singale linked list because we don't have back address
ReplyDeleteOne way to do this is using recursive operation
ReplyDeleteOther way is through LIFO data structure
Ya I agree, we can use Stack, and push the elements in stack. and after the POP it till the stack is empty.
ReplyDeleteCan we not write recursive program like
ReplyDeletevoid PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(Root->next)
PrintListFromBack(Root->Next);
printf("%d", Root->info);
}
1] Stack solution
ReplyDelete2] Recursive function
3] Reverse list and print
build another list.
ReplyDelete//pass head as the argument
ReplyDeleteint print_reverse(list *ptr){
if(ptr==NULL)
return 0;
else{
print_reverse(ptr->link);
printf(" %d ",ptr->data);
}
return 0;
}
first reverse the list and read
ReplyDeletealgorithm to reverse a singly linked list:
ReplyDeletetop=null
while(head!=null)
{
temp = start
start = start.next
temp.next = top
top = temp
}
correct, no?
CORRECT SOLUTION IS
ReplyDeletevoid PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(Root->next)
PrintListFromBack(Root->Next);
printf("%d", Root->info);
}
Copied from cracktheinterview. word to word
ReplyDeleteThe solution given with the function PrintListFromBack is a good one and make sense.
ReplyDeleteone way void readfrmbck(struct node *start)
ReplyDelete{
if(start==NULL)
return;
//printf("%d",start->data);
readfrmbck(start->next);
printf("%d",start->data);
}to solve this question is:
one way to solve this is:
ReplyDeletevoid readfrmbck(struct node *start)
{
if(start==NULL)
return;
//printf("%d",start->data);
readfrmbck(start->next);
printf("%d",start->data);
}