Output linked list from tail to head

Source: Internet
Author: User

The structure of the linked list is:

struct ListNode

{

int M_nkey;

Listnode* Next;

};

From the beginning to the end of the output chain list for everyone is relatively simple, but from the tail should be how to do it?

In fact, you can think of this, from beginning to end the chain list, and put the data of the linked list into the stack, when traversing to the tail of the list, and then output data from the stack, the resulting value is from the tail to output the value of the head,

But in this implementation, it is necessary to maintain a stack, but also very troublesome, at this time you can think about the implementation of the stack and recursive implementation of the same, then it is possible not to use the stack, but the use of recursive implementation?

Is it easier to understand and not to maintain stacks? This is possible.

1 voidReverselist (listnode*plisthead)2 {3         if(Plisthead! =NULL)4         {5               6                 if(Plisthead->m_pnext! =NULL)7                 {8Reverselist (plisthead->next);9                 }Ten  One                 Aprintf"%d", plisthead->m_nkey); -         } -}

Output linked list from tail to head

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.