Python Reverse List

Source: Internet
Author: User

Flip a linked list

Have you ever encountered this problem in a real interview? Yes

Sample Example

Given a list of 1->2->3->null, this flipped list is 3->2->1->null

The steps are this:

1. Create a new empty node: None
2.1->none
3.2->1->none
4.3->2->1->none

The code is very simple:

1 #-*-coding:utf-8-*-2 #class ListNode:3 #def __init__ (self, x):4 #self.val = x5 #Self.next = None6 classSolution:7     #back to ListNode8     defreverselist (Self, phead):9         #Write code hereTentemp =None One          whilePhead: ACur =Phead.next -Phead.next =Temp -temp =Phead theHead =cur -         returnTemp

Python Reverse 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.