The sword refers to offer 3. Print linked list from tail to head (linked list)

Source: Internet
Author: User

Title Description

Enter a list that returns a ArrayList in the order of the list values from the end of the header.

Title Address

https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage=1&rp=2 &ru=%2fta%2fcoding-interviews&qru=%2fta%2fcoding-interviews%2fquestion-ranking

Ideas

Using the Python library function, create a new list, insert every time with insert to the front, or use append to use reverse.

#-*-coding:utf-8-*-classListNode:def __init__(self, x): Self.val=x Self.next=None#unidirectional chain table node1:1->2->3Node1 = ListNode (1) Node2= ListNode (2) Node3= ListNode (3) Node4= ListNode (4) Node1.next=Node2node2.next=Node3node3.next=Node4classSolution:defPrintlistfromtailtohead (Self, listnode):#Method 1: Use the Insert function        #c = []        #While ListNode:        #C.insert (0,listnode.val)        #ListNode = Listnode.next        #return C        #Method 2: Use Append, and finally reversec = []         whilelistnode:c.append (listnode.val) ListNode=listnode.next c.reverse ()returnCif __name__=='__main__': Run=solution () result=Run.printlistfromtailtohead (Node1)Print(Result)

The sword refers to offer 3. Print linked list from tail to head (linked list)

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.