[LeetCode-interview algorithm classic-Java implementation] [092-Reverse Linked List II (Reverse Single-Chain List II)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [092-Reverse Linked List II (Reverse Single-Chain List II)], leetcode -- java
[092-Reverse Linked List II (Reverse single-chain table II )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given1->2->3->4->5->NULL,m = 2Andn = 4,
Return1->4->3->2->5->NULL.
  Note:
Given m, n satisfy the following condition:
  1 ≤ m ≤ n ≤ lengthOf list.

Theme

Given a single-chain table, convert the elements between m and n.
The given n and m are valid and are solved using the in-situ method (using constant auxiliary space)

Solutions

First, find the prev of the first element to be reversed, then calculate the number of elements to be reversed, insert the element into the header, insert it behind the prev, and keep the linked list continuously open.

Code Implementation

Linked List Node Type

public class ListNode {    int val;    ListNode next;    ListNode(int x) { val = x; }}

Algorithm Implementation class

Public class Solution {public ListNode reverseBetween (ListNode head, int m, int n) {ListNode root = new ListNode (0); ListNode p = root; root. next = head; for (int I = 1; I <m & p! = Null; I ++) {p = p. next;} if (p! = Null) {ListNode q = p. next; ListNode r; // if m is a negative number, it is considered to be switching from the first one if (m <1) {m = 1;} n = n-m + 1; // n indicates the number of nodes to be changed. // use the end insertion method only when there are two nodes. The number of end inserts is n-1 for (int I = 1; I <n & q. next! = Null; I ++) {// is the node r = q. next; // perform the end insertion Operation after the q node. next = r. next; r. next = p. next; p. next = r;} head = root. next;} return head ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47310547]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.