Leetcode: reverse Linked List II

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 ≤ m ≤ n ≤ length of list.

Analysis: I have been thinking about this question for a long time. First, I got a wrong understanding of the question. I thought it was just a comparison between M and N, the result shows that all elements between M and N need to be exchanged. For a moment, I didn't think of how to do reverse better. I took a look at the idea on the Internet and found that it would be better to use runner technique or dummy node; two pointers: npointer refers to the position of N, and mpointer refers to the previous position of M. Each time, the elements of the last position of mpointer are placed in the next position of npointer: mpointer. next. next = npointer. next; until mpointer. next = npointer until (M and N overlap)

Original linked list: 1-> 2-> 3-> 4-> 5-> 6-> 7; M = 3, n = 6

Step 1: 1-> 2-> 4-> 5-> 6-> 3-> 7

Step 2: 1-> 2-> 5-> 6-> 4-> 3-> 7

......

Result: 1-> 2-> 6-> 5-> 4-> 3-> 7

Note that pointer m is switching to right one by one in each step, but pointer n remains no change.

It shows the benefits of using dummy node in the linked list and performing operations on the current node. Next.

 

Notice: Like this kind of linked list deletion and insertion Operation, before deleting and inserting, it is best to copy the next node of the deleted node and the next node at the insertion location, store them in a variable listnode store and directly access this variable, instead of using a variable similar to mpointer. the next method stores them, because mpointer is subject to this, and once mpointer changes, the next node of the delete/Insert node cannot be found.

 1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { 7  *         val = x; 8  *         next = null; 9  *     }10  * }11  */12 public class Solution {13     public ListNode reverseBetween(ListNode head, int m, int n) {14         ListNode prev = new ListNode(-1);15         prev.next = head;16         ListNode mpointer = prev; //point to m-1 position17         ListNode npointer = prev; //point to n position18         while (m > 1) {19             mpointer = mpointer.next;20             m--;21         }22         while (n > 0) {23             npointer = npointer.next;24             n--;25         }26         while (mpointer.next != npointer) {27             ListNode mnext = mpointer.next.next;28             ListNode nnext = npointer.next;29             mpointer.next.next = nnext;30             npointer.next = mpointer.next;31             mpointer.next = mnext;32         }33         return prev.next;34     }35 }

 

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.