Flip Chain list II

Source: Internet
Author: User

Title Description: Flips the section m nodes in the linked list to the nth node; note: M,n satisfies 1≤m≤n≤ chain table length

Example: Given the list 1->2->3->4->5->null, M = 2 and n = 4, return 1->4->3->2->5->null


Previously, it has been implemented in the normal, for the entire list of flip (see: Click to open the link ), this adds a bit of difficulty and sets the position of the rollover. But the basic method has not changed. The difficulty is mainly in the "chain" and "link" operation.

Therefore, we may wish to follow the previous "Palindrome list" (see: Click to open the link) solution, the list is broken, and then processed. This is a lazy way to reduce the difficulty of the problem.

Simply put, take a sample example, you can find the starting position of the flip, here is 2, and then according to this position will be broken list: 1->null, 2->3->4->5->null, which formed two linked lists.

Then, take the second linked list to the head node in turn, and put it behind 1:

1.1->2->null, 3->4->5->null

2.1->3->2->null, 4->5->null

。。。

This cycle is done several times, looping 3 times, that is N-m + 1 times

Then merge the current two linked lists. The code can be obtained as follows:

"" "Definition of Listnodeclass ListNode (object): Def __init__ (Self, Val, next=none): Self.val = Val Self. Next = Next "" "Class Solution:" "@param head:the head of linked list @param m:start position @param n:end  Position "" "Def Reversebetween (self, head, M, n): dummy = ListNode ( -1) Dummy.next = head pre =        Dummy count = 1 # Find the start of the section you want to flip while count! = M:pre = Pre.next Count + = 1 # Gap for the number of cycles gap = n-m + 1 # The second part second = Pre.next # Sets the tail node of the first part, with the intention of the final merge tail = SE  Cond Pre.next = None while gap! = 0:cur = Second second = Second.next temp = Pre.next Pre.next = cur Cur.next = temp gap-= 1 # Two-part Merge Tail.next = sec Ond return dummy.next # write your code here


Flip Chain list II

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.