【leetcode】92. Reverse Linked List II

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.

Tips:给定一个链表,与两个整数m ,n;

从m位置开始,到n位置结束,反转这段链表,返回翻转后的链表。

思路:先让pre指针向前走m-1个位置,记住当前的结点 NodeBeforeM。

在让pre指针想下走一个位置到达m处,记下当前结点为nodeM

m+1位置的结点记为 cur

从m~n 通过改动指针的方式来翻转这段链表,代码如下

for(int i=m;i<n;i++){
                ListNode next=cur.next;
                cur.next=pre;
                pre=cur;
                cur=next;
            }

结束循环之后,

pre结点指向n位置的结点

cur结点指向n+1位置的结点

再将NodeBeforeM cur结点与翻转后的结点 前后连接起来 就可以了。

整体代码如下:

package medium;

import dataStructure.ListNode;

public class L92ReverseLinkedListII {
     public ListNode reverseBetween(ListNode head, int m, int n) {
         if(head==null ||m<=0||n<=0||m>n)
             return null;
         if(m==n) return head;
            ListNode node = new ListNode(0);
            ListNode pre=node;
            pre.next=head;
            node=pre;
            for(int i=1;i<m;i++){
                pre=pre.next;
            }
            ListNode nodeBeforeM=pre;
            System.out.println("nodeBaforeM:"+nodeBeforeM.val);
            pre=pre.next;
            ListNode nodeM=pre;
            System.out.println("nodeM:"+nodeM.val);
            ListNode cur=pre.next;
            System.out.println("cur"+cur.val);
            for(int i=m;i<n;i++){
                ListNode next=cur.next;
                cur.next=pre;
                pre=cur;
                cur=next;
            }
            System.out.println("!!!!!!!!!!!!");
            System.out.println("pre"+pre.val);
            System.out.println("cur"+cur.val);
            System.out.println("next"+cur.next.val);
            System.out.println("!!!!!!!!!!!!");
            nodeBeforeM.next=pre;
            nodeM.next=cur;
            return node.next;
        }
     public static void main(String[] args) {
        L92ReverseLinkedListII l92 = new L92ReverseLinkedListII();
        ListNode head1 = new ListNode(1);
        ListNode head2 = new ListNode(2);
        ListNode head3 = new ListNode(3);
        ListNode head4 = new ListNode(4);
        ListNode head5 = new ListNode(5);
        ListNode head6 = new ListNode(6);
        ListNode head7 = new ListNode(7);
        ListNode head8 = new ListNode(8);
        ListNode head9 = new ListNode(9);
        ListNode head10=null;
        head1.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        head5.next = head6;
        head6.next = head7;
        head7.next = head8;
        head8.next = head9;
        head9.next = head10;
        int n=4 ,m=2;
        ListNode head = l92.reverseBetween(head1, m, n);
        while(head!=null){
            System.out.println(head.val);
            head=head.next;
        }
    }
}

输出结果如下:

nodeBaforeM:1
nodeM:2
cur3
!!!!!!!!!!!!
pre4
cur5
next6
!!!!!!!!!!!!
m=2;~~~~~n=4;
1 ->2 ->3 ->4 ->5 ->6 ->7 ->8 ->9 
~~~~~~~~~~~~~~~~~~~~~~~
1 ->4 ->3 ->2 ->5 ->6 ->7 ->8 ->9 

 

posted @ 2018-02-04 21:35  于淼  阅读(201)  评论(0编辑  收藏  举报