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);}
Post a Comment
14 comments:
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 operation
Other 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 like
void PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(Root->next)
PrintListFromBack(Root->Next);
printf("%d", Root->info);
}
1] Stack solution
2] Recursive function
3] Reverse list and print
build another list.
//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;
}
first reverse the list and read
algorithm to reverse a singly linked list:
top=null
while(head!=null)
{
temp = start
start = start.next
temp.next = top
top = temp
}
correct, no?
CORRECT SOLUTION IS
void PrintListFromBack(LIST * Root)
{
if(!Root)
return;
else
if(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);
}
Post a Comment