Print the linked list from the end to the end. You cannot reverse the source linked list.

Source: Internet
Author: User

Method 1: implement with the stack, step backward from the first node, and then push the node pointer (Address) into the stack in sequence. After all traversal ends, extract the content from the top of the stack and print the data. Since the later Node Address is above the stack, the printing order is printed from the end.

Voidprint (node * List)

{Stack <node *> node; node * P = List; while (P! = NULL) {node. Push (p); P = p-> next;} while (! Node. Empty () {P = node. Top (); cout <p-> data <","; node. Pop ();}}

 

 

Method 2: implement with recursionCodeConcise, but when the linked list is long, it will lead to a deep hierarchy of recursive calls, which may cause function call stack overflow. Stack performance is better, and recursion is used with caution.

Void print (node * List) {node * P = List; If (P! = NULL) {If (p-> next! = NULL) {print (p-> next) ;}cout <p-> data <",";}}

 

 

 

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.