92. Reverse Linked List II

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode pre=new ListNode(0);
        pre.next=head;
        ListNode p=pre;
        for(int i=0;i<m-1;i++)
            p=p.next;
        int s=n-m;
        ListNode q=p.next;
        while(s>0)
        {
            ListNode r=q.next;
            q.next=q.next.next;
            r.next=p.next;
            p.next=r;
            s--;
        }
        return pre.next;
    }
}

 

posted @ 2017-09-28 13:23  Weiyu Wang  阅读(96)  评论(0编辑  收藏  举报