Lintcode Easy title: Reverse Linked list Flip linked list

Source: Internet
Author: User
Tags lintcode

Topic:

Flip Linked list

Flip a linked list

Sample Example

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

challenges

One-time rollover complete in situ

Solving:

A recursive return can be solved.

Java Program:

/*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; *     } * } */  Public classSolution {/**     * @paramhead:the head of linked list. * @return: The new head of reversed linked list. */     PublicListNode Reverse (listnode head) {//Write your code here        if(Head = =NULL|| Head.next = =NULL)            returnHead; ListNode Second=Head.next; Head.next=NULL; ListNode Res=reverse (second); Second.next=Head; returnRes; }}
View Code

Total time: 2079 Ms

Python program:

"""Definition of Listnodeclass ListNode (object): Def __init__ (Self, Val, next=none): Self.val = Val Self . Next = Next"""classSolution:"""@param head:the First node of the linked list.                   @return: You should return the head of the reversed linked list.    Reverse it in-place. """    defreverse (self, head):#Write your code here        ifHead = = NoneorHead.next = =None:returnHead Second=Head.next; Head.next=None Res=Self.reverse (second) Second.next=HeadreturnRes
View Code

Python submission and the same as the title has been pending, should be the background maintenance of what and so on .... The above program is not tested.

Lintcode Easy title: Reverse Linked list Flip 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.