[Leetcode] Reverse Linked List II inverted linked list two

Source: Internet
Author: User

Reverse a linked list from position m to N. Do it in-place and in One-pass.

For example:
Given 1->2->3->4->5->NULL , m = 2 and n = 4,

Return 1->4->3->2->5->NULL .

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤length of list.

It is strange why there is not one of the inverted list, it comes to this inverted list of two, but guess also can guess one is simply inverted the entire list, and this is the extension of the place is inverted one of the small section. For the list of problems, according to previous experience is generally to build a dummy node, linked to the original list of the head node, so that even if the head node changes, we can also be dummy->next to get the new list of the head node. The requirement of this problem is only through a traversal, take the example of the problem, the transformation is 2,3,4 these three points, then we can first remove 2, with the front pointer to 2, and then when the 3 is removed, we add 3 to the front of 2, put the front pointer forward to 3, and so on, to 4 after the stop, So we get a new linked list 4->3->2, the front pointer pointing to 4. For the original linked list, there are two points of position is very important, need to be recorded with pointers, respectively, 1 and 5, because when 2,3,4 is taken, the original linked list becomes 1->5->null, to insert the new list when the two points need to position. 1 of the location is very easy to find, because we know the value of M, we use the pre pointer to record 1 of the position, 5 of the position to record, when the 4 node is taken away, 5 of the position needs to be written down, so that we can be inverted after the small chain list into the original list. The code is as follows:

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*reversebetween (ListNode *head,intMintN) {ListNode*dummy =NewListNode (-1); Dummy->next =Head; ListNode*cur =dummy; ListNode*pre, *front, *Last ;  for(inti =1; I <= M-1; ++i) cur = cur->Next; Pre=cur; Last= cur->Next;  for(inti = m; I <= N; ++i) {cur= pre->Next; Pre->next = cur->Next; Cur->next =Front; Front=cur; } cur= pre->Next; Pre->next =Front; Last->next =cur; returnDummy->Next; }};

[Leetcode] Reverse Linked List II inverted linked list two

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.