You are not logged in. Please login at www.codechef.com to post your questions!

×

LinkedList_Problem

How to print the value contained in linked list in reverse order??

asked 03 Aug '15, 18:25

chaman_amit's gravatar image

1★chaman_amit
871413
accept rate: 0%

edited 03 Aug '15, 18:26


This algorithm would work

int func(struct node* temp){
    if (temp==NULL) return 0;
    func(temp->next);
    printf("%d ",temp->data);
    return 0;
}

or

add another pointer that points to the previous element in the list

or

traverse through the linked list and keep on adding elements to an array print the array in reverse order

I hope it helped!

link

answered 03 Aug '15, 19:07

prrateekk's gravatar image

3★prrateekk
534216
accept rate: 12%

@prrateekk ..no problem at all it works fine..

actually recursive calls also store in stack till you will not get last function call..

but beginner it is quite tough to understand how recursion going on....

that's why i have shown him to demo of stack seprately....

hope you got it...

happy coding

link

answered 04 Aug '15, 15:13

rcsldav2017's gravatar image

5★rcsldav2017
1.1k1229
accept rate: 6%

  • if you think that recursive calls is not so visible..how this magic going on than just do one thing

  • just traverse the linked list and push element into stack


   while(stack not empty){

      printf("%d ",stack.top);

       stack.pop();
      }

or as @prrateekk mention use array and store all value as you iterate and 
print array element in  reverse order..

hope it helps you

HAPPY CODING

link

answered 04 Aug '15, 00:58

rcsldav2017's gravatar image

5★rcsldav2017
1.1k1229
accept rate: 6%

Is there any problem with my recursive code? As I tested it, it works fine. If there's any problem, please let me know

(04 Aug '15, 09:50) prrateekk3★

Yeah that's right. After all functions execute on stack

link

answered 04 Aug '15, 17:15

prrateekk's gravatar image

3★prrateekk
534216
accept rate: 12%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3

question asked: 03 Aug '15, 18:25

question was seen: 669 times

last updated: 04 Aug '15, 17:15

Related questions