"Algorithmic programming C + + Python" single-linked list reverse output

Source: Internet
Author: User

Title Description Enter a list of values for each node of the list to print from the end of the header. The following methods only implement the function, not necessarily the best. In the cattle-guest network test, C++:3MS 480kpython:23ms 5732k
/** * struct ListNode {* int val;* struct ListNode *next;* listnode (int x):* val (x), Next (NULL) {*}*};*/classSolution { Public: Vector<int> Printlistfromtailtohead (listnode*head) {Vector<int>Vec_output; if(head = = NULL) {returnVec_output;}  Do{vec_output.push_back (head-val); Head= head->Next; } while(head!=NULL);        Reverse (Vec_output.begin (), Vec_output.end ()); returnVec_output; }};

Python:

#-*-coding:utf-8-*-#class ListNode:#def __init__ (self, x):#self.val = x#Self.next = NoneclassSolution:#returns a sequence of list values from the tail to the head, for example [ .    defPrintlistfromtailtohead (Self, ListNode): List_val= []         while(True):ifListNode = = None:return[] List_val.append (Listnode.val)if(listnode.next): ListNode=Listnode.nextElse:                 Break        returnLIST_VAL[::-1]

"Algorithmic programming C + + Python" single-linked list reverse output

Related Article

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.