GeeksforGeeks » Interview Questions
Amazon Interview Question for Software Engineer/Developer (Fresher) about Linked Lists
(5 posts)-
Print Reverse of linked list (dont reverse it) with only constant space.
-
#include<stdio.h> typedef struct mynode { int data; struct mynode *next; }node; void printListInReverse(node *head); int main() { //assume that list is already created and head points to head of linked list printListInReverse(head); } void printListInReverse(node *head) { if(head->next != NULL) printListInReverse(head->next); printf("%d",head->data); } -
void ReversePrint(node* head)
{
if (!head) return;
ReversePrint(head->next);
cout << head->data;
} -
The above two solutions use recursion, so the stack will still occupy O(n) space, since there will be n recursive calls.
-
The above two solutions use recursion, so the stack will still occupy O(n) space, since there will be n recursive calls.
-
how about reversing the list first, printing it, and then reverse it again.. wont need the stack if done using loop
Reply
You must log in to post.