GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer (Fresher) about Linked Lists

(5 posts)

Tags:

  1. lalit
    guest
    Posted 7 months ago #

    Print Reverse of linked list (dont reverse it) with only constant space.

  2. Rakesh
    guest
    Posted 7 months ago #

    #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);
    
    }
    
  3. ygos
    guest
    Posted 7 months ago #

    void ReversePrint(node* head)
    {
    if (!head) return;
    ReversePrint(head->next);
    cout << head->data;
    }

  4. tiputiger
    guest
    Posted 5 months ago #

    The above two solutions use recursion, so the stack will still occupy O(n) space, since there will be n recursive calls.

  5. tiputiger
    guest
    Posted 5 months ago #

    The above two solutions use recursion, so the stack will still occupy O(n) space, since there will be n recursive calls.

  6. anamika sharma
    Member
    Posted 4 months ago #

    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.

RSS feed for this topic